Cookie Preferences

We use cookies to enhance your experience. Choose your preference for cookie usage.

Essential cookies are required for basic functionality. Additional cookies help us improve our service and provide analytics.

View third-party services
  • Google Analytics: Website traffic analysis and conversion tracking
  • RudderStack: Analysis of user interactions with the website
  • Microsoft Clarity: User behavior analysis & session recordings
  • Facebook Pixel: Marketing analysis of user activity
KotlinBasic operationsTest topic C 20_ R2

Test topic A 20_ R2

At this point, you have already heard about Python strings and probably gained some experience using them. However, the Python standard library also has the string module that offers additional tools to work with strings. One way or another, programmers interact with strings rather often, so let's expand our knowledge about this Python domain using the string module.

Constants

Since string is a module, we need to import it first:

import string

The first tool we are going to look at is the string constants. They are basically strings that contain a certain type of characters — punctuation marks, digits, uppercase English alphabet, and so on. The string module employs ASCII characters only. ASCII (American Standard Code for Information Interchange) is one of the character encoding standards used in electronics.

Below is a table with the names and contents of every string module constants:

Name Description Contents
string.ascii_lowercase the lowercase English alphabet abcdefghijklmnopqrstuvwxyz
string.ascii_uppercase the uppercase English alphabet ABCDEFGHIJKLMNOPQRSTUVWXYZ
string.ascii_letters the concatenation of ascii_lowercase and ascii_uppercase abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
string.digits numbers from 0 to 9 0123456789
string.octdigits digits used in the octal number system that can represent any number with 8 digits 01234567
string.hexdigits digits used in the hexadecimal number system that can represent any number with 16 digits 0123456789abcdefABCDEF
string.punctuation a set of characters for punctuation marks !"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~
string.printable the printable Python ASCII characters – digits, ascii_letters, punctuation, and whitespace 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c
string.whitespace the whitespace ASCII characters – a space, a tab, a linefeed, a return, a formfeed, and a vertical tab '\t\n\r\x0b\x0c

We can access them with a simple command:

print(string.ascii_letters)
# 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

print(string.digits)
# '0123456789'

You can use these constants to check whether a character of interest belongs to one of the groups above, to remove or add the characters from your strings, or to distinguish them in someone else's code. Let's say you've written a calculator and want to test if the input contains only digits. To do that, you can use a membership operator in that will return a boolean value True if your item is in the sequence or False if otherwise:

user_input = input('Enter one digit: ')  # '5'

print(user_input in string.digits)  # True

The helper function

There is only one helper function in the module: string.capwords(s, sep). It capitalizes each word in a string. You can also provide your own separator; the default value is a space:

my_str = "the string i want to capitalize"
print(string.capwords(my_str))  # 'The String I Want To Capitalize'

What is considered by a "word" depends on the separator — it may be everything in between:

my_other_str = "another,string,i,want,to,capitalize"

print(string.capwords(my_other_str, sep=","))  # 'Another,String,I,Want,To,Capitalize'
print(string.capwords(my_other_str))  # 'Another,string,i,want,to,capitalize'

In the first case, the separator is a comma, so each piece of text between the commas is capitalized. In the second case, the separator is a space, so there's only one "word" that is capitalized.

Formatting

The string module also suggests a few advanced ways to string formatting. For instance, you can customize your own string with the built-in Formatter class. The ability to change the default way of formatting is very useful but quite specific, so we won't consider these things in the topic, but you're more than welcome to learn about them yourself.

Anyway, we will take a look at template strings. They are another tool for string formatting. As the name suggests, it can create string templates to fill them in later:

my_template = string.Template("Hello, $name!")
my_str = my_template.substitute(name="Jack")
print(my_str)  # 'Hello, Jack!'

Pretty convenient, right? You can use them if you want to generate the same string with a couple of differences, for greetings, for example.

Conclusion

In this topic, we have learned a little more about Python strings by investigating the string module from the standard Python library. It has predefined string constants with groups of characters that can be used for membership tests. It contains the string.capwords() function that capitalizes each word in a string which can be beneficial sometimes. And, finally, this module suggests several advanced ways to string formatting. Hopefully, you'll find this new information useful in practice. Speaking of which, let's resolve some challenges!

How did you like the theory?
Report a typo