In this article, we are going to learn the Python string module. the string is a built-in module in Python which is used to precess the string.
the string is a built-in module, That means the string is pre-installed with Python. In the previous tutorial, we have seen the Python string HTML module.
To understand this example you should have basic knowledge of Python Programming.
Headings of Contents
Python String Module:
Python provides a built-in module named string which provides lots of functions and properties to precess the string.
Python string module is a built-in Python module that means you don’t need to install it by using pip. To use the string module in Python you need to import using the import keyword.
Python String Module Properties:
Let’s look constants, define in the string module.
string.ascii_letters
This constants return the combination of ascii.lowercase and ascii.uppercase letters.
import string
x = string.ascii_letters
print(x)
Output
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
string.ascii_lowercase:
string.ascii_lowercase constants return the lowercase letters.
import string
x = string.ascii_lowercase
print(x)
Output
abcdefghijklmnopqrstuvwxyz
string.ascii_uppercase:
string.ascii_uppercase constants return the upper case letters.
import string
x = string.ascii_uppercase
print(x)
Output
ABCDEFGHIJKLMNOPQRSTUVWXYZ
string.digits:
string.digits return the digits.
import string
x = string.digits
print(x)
Output
0123456789
string.hexdigits:
The string.hexdigits return the hexadecimal:
import string
x = string.hexdigits
print(x)
Output
0123456789abcdefABCDEF
string.octdigits:
The string.octdigits return the hexadecimal.
import string
x = string.hexdigits
print(x)
Output will be:- 01234567
string.punctuation:
The string.punctuation return the punctuations.
import string
x = string.punctuation
print(x)
Output
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
string.whitespace:
The string.whitespace return the whitespaces.
import string
x = string.whitespace
print(x)
string.printable:
The string.printable constants return the combination of ascii_letters, digits, punctuations and whitespace.
import string
x = string.printable
print(x)
Output
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
Python string module Helper Functions:
string module provides function which are used to process the string.
capwords(s, sep = None)
The string.capwords() helper function is used to split the string into the words using str.split() function. Then it capitalizes each word using str.capitalize() function. Finally, it joins the capitalized words using str.join().
Example 1:
import string
txt = "Programming Funda is programming portal"
x = string.capwords(txt)
print(x)
Output will be:- Programming Funda Is Programming Portal
Example 2:
import string
txt = " Programming Funda is programming portal "
x = string.capwords(txt, "g")
print(x)
Output
progRamming funda is progRamming portal
Python string module Classes:
Python built-in string module provide two class Formatter and Template.
Formatter:
Formatter class is used to format the string same as str.format() function.
from string import Formatter
formatter = Formatter()
print(formatter.format('{portal}', portal = 'ProgrammingFunda is a portal'))
print(formatter.format('{} {portal}', 'Programming Funda', portal = 'is a programming portal'))
print('{} {portal}'.format('Welcome to the', portal = 'programming Funda'))
Output
ProgrammingFunda is a portal
Programming Funda is a programming portal
Welcome to the programming Funda
Template:
Template class is used to create the string template for simple string substitute.
from string import Template
temp = Template("$name is the Founder of $portal")
s = temp.substitute(name = "Vishvajit Rao", portal = "Programming Funda")
print(s)
Output
Vishvajit Rao is the Founder of Programming Funda
Conclusion:
In this tutorial, you have learned about the Python string module. string module in Python provides lots of functions that are used to process the string. This is the legitimate module to work with string.
I hope this tutorial will help you, if you like this article, please comment and share with your friends who want to learn Python programming from scratch to advanced.
For More Information:- Click Here