Is there a reason to import a string module in Python?

I'm starting to program, and Python is my first language. I am using the Python shell right now, but I don’t understand why we need to import the string module.

I know that importing a string imports some functions, but when I tried to use functions like string.split and string.join , all of them work without importing, so I assume that this means they are just built in to Python.

Is there anything that works after importing the string module that would not work otherwise?

+6
source share
3 answers

As a rule, you do not need to import the string module, since the class is already built-in. However, there are several constants that are in the string module that are not inline, which can be useful.

+2
source

These days there is no need. Most likely, you will not need to use import string .

You can find the list of components here inside the string library, but as said, most of them have been added to the rest of the standard library. This is just the way it was created in Python many years ago. string basically has a bunch of constants.

From comments in the source file

A collection of string operations (most of them are no longer in use).

Note: most of the code that you see here is usually not currently in use. Starting with Python 1.6, many of these functions are implemented as methods on a standard string object. They used a built-in module called strop, but strop is now deprecated by itself.

+1
source

Typically, no, there is nothing that "works after you import a string module that will not work otherwise" to answer your question.

In addition to giving you access to potentially useful constants, as mentioned, the documentation also specifies the Formatter class in the line module, which allows you to create and customize your own string formatting methods using the same implementation as the built-in format (). That way, if you really wanted to get an idea of ​​custom string formatting, it could be using string import.

0
source

All Articles