Report this

What is the reason for this report?

Python String Module

Published on August 3, 2022
Python String Module

Python String module contains some constants, utility function, and classes for string manipulation.

Python String Module

It’s a built-in module and we have to import it before using any of its constants and classes.

String Module Constants

Let’s look at the constants defined in the string module.

import string

# string module constants
print(string.ascii_letters)
print(string.ascii_lowercase)
print(string.ascii_uppercase)
print(string.digits)
print(string.hexdigits)
print(string.whitespace)  # ' \t\n\r\x0b\x0c'
print(string.punctuation)

Output:

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789
0123456789abcdefABCDEF
 	

!"#$%&'()*+,-./:;?@[\]^_`{|}~

python string module constants

string capwords() function

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.capitalize() function. Finally, it joins the capitalized words using str.join(). If the optional argument sep is not provided or None, then leading and trailing whitespaces are removed and words are separated with single whitespace. If it’s provided then the separator is used to split and join the words.

s = '  Welcome TO  \n\n JournalDev '
print(string.capwords(s))

Output: Welcome To Journaldev python string capwords

Python String Module Classes

Python string module contains two classes - Formatter and Template.

Formatter

It behaves exactly same as str.format() function. This class become useful if you want to subclass it and define your own format string syntax. Let’s look at a simple example of using Formatter class.

from string import Formatter

formatter = Formatter()
print(formatter.format('{website}', website='JournalDev'))
print(formatter.format('{} {website}', 'Welcome to', website='JournalDev'))

# format() behaves in similar manner
print('{} {website}'.format('Welcome to', website='JournalDev'))

Output:

Welcome to JournalDev
Welcome to JournalDev

python string module Formatter class

Template

This class is used to create a string template for simpler string substitutions as described in PEP 292. It’s useful in implementing internationalization (i18n) in an application where we don’t need complex formatting rules.

from string import Template

t = Template('$name is the $title of $company')
s = t.substitute(name='Pankaj', title='Founder', company='JournalDev.')
print(s)

Output: Pankaj is the Founder of JournalDev. Python string module Template class

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author

Pankaj Kumar
Pankaj Kumar
Author
See author profile

Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Was this helpful?

I didn’t get the thing searched by me

- Mayank

Wow thanks.

- Rohit

Is there any way to just grab the vowels?

- Adan

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.