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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With