Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

string

I am a beginner in programming and Python is my first language. I am using a Python shell right now, but don't understand why we need to import the string module.

I know that importing string imports some functions, but when I tried using functions like string.split and string.join, they all work without the import, so I assume that means that they are just Python builtins.

Is there anything that works once you import the string module that wouldn't work otherwise?

like image 516
user3564573 Avatar asked Apr 23 '14 14:04

user3564573


People also ask

What is the use of string module in Python?

Python string module contains a single utility function - capwords(s, sep=None). This function split the specified string into words using str. split(). Then it capitalizes each word using str.

Why do we need to import modules?

We use modules to break down large programs into small manageable and organized files. Furthermore, modules provide reusability of code. We can define our most used functions in a module and import it, instead of copying their definitions into different programs.

What is the use of import module in Python?

Import in python is similar to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is the most common way of invoking the import machinery, but it is not the only way.


1 Answers

Generally you don't need to import string module as the class is already in builtins. However, there are several constants that are in the string module that are not built in, that can be usefull.

like image 80
cmd Avatar answered Oct 25 '22 18:10

cmd