0

What are the reasons why one should avoid the string module and use str instead in Python? Anybody knows? Thanks a lot for the help.

2
  • 1
    Deprecated, and there is no reason to use something like string.capitalize("word") when you can just do "word".capitalize(). Unless, of course, using the string module caters to a specific need. Commented Mar 12, 2017 at 4:12
  • Where did you see this advice? Commented Mar 12, 2017 at 15:12

1 Answer 1

2

There is a bunch of deprecated functions in the string module clearly tagged as such in Python2 (https://docs.python.org/2.7/library/string.html#deprecated-string-functions). These function are unavailable in Python3. Obviously, these functions should not be used. Let's now talk about the rest.

The existence of deprecated functions does not mean that the whole string library module is to be avoided in any way.

Further, you don't have a real choice between str class and string module. Take for example the functionality of Template and Formatter. The former provides "$name" substitution and the latter allows to parse format strings and to customize string formatting. These are uncommon tasks, but if you need to do just that, the str class alone does not provide the right tools.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried 'import string' in Pythn 3.5 and it worked, appears not deprecated.
@DjangoUrl, VPfB isn't saying the entire module is deprecated. "The existence of deprecated functions does not mean that the whole string library module is to be avoided in any way." Some of the functions in the module are, e.g. atof(), atoi(), atol().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.