string
The Python string
module provides a collection of string constants and utility functions for common string operations.
It includes predefined string constants, which can be particularly useful for tasks involving the classification and manipulation of characters.
Here’s a quick example:
>>> import string
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
Key Features
- Provides a set of string constants such as
ascii_letters
,digits
,punctuation
- Offers utility functions like
capwords()
for string manipulation - Facilitates character classification with constants like
ascii_lowercase
,whitespace
Frequently Used Classes and Functions
Object | Type | Description |
---|---|---|
string.ascii_letters |
Constant | Contains the concatenation of ascii_lowercase and ascii_uppercase |
string.digits |
Constant | Contains the string digits ‘0123456789’ |
string.punctuation |
Constant | Contains string of ASCII characters that represent punctuation |
string.printable |
Constant | Contains the characters considered printable |
string.hexdigits |
Constant | Represents the characters that compose the hexadecimal notation ‘0123456789abcdefABCDEF’ |
string.octdigits |
Constant | Contains the characters that represent the octal notation ‘01234567’ |
string.whitespace |
Constant | Contains string containing all ASCII whitespace characters |
string.capwords() |
Function | Capitalizes the first letter of each word |
Examples
Capitalize the first letter of each word in a string:
>>> import string
>>> string.capwords("hello, world!")
'Hello, World!'
Check for ASCII letters:
>>> import string
>>> all(c in string.ascii_letters for c in "Python")
True
Common Use Cases
- Generating random strings using predefined character sets
- Formatting and manipulating text data
- Validating and classifying characters in strings
Real-World Example
Suppose you need to generate a random string of a given length consisting of letters and digits. You can use the string
module to easily access the necessary character sets:
>>> import string, random
>>> def random_string(length=8):
... characters = string.ascii_letters + string.digits
... return "".join(random.choices(characters, k=length))
...
>>> random_string()
'3hJ5kL8P'
In this example, you use the string
module to construct a pool of characters and generate a random string.
Related Resources
Tutorial
Strings and Character Data in Python
In this tutorial, you'll learn how to use Python's rich set of operators and functions for working with strings. You'll cover the basics of creating strings using literals and the str() function, applying string methods, using operators and built-in functions with strings, and more!
For additional information on related topics, take a look at the following resources:
- How to Join Strings in Python (Tutorial)
- How to Split a String in Python (Tutorial)
- A Guide to Modern Python String Formatting Tools (Tutorial)
- How to Check if a Python String Contains a Substring (Tutorial)
- Strings and Character Data in Python (Course)
- Python Strings and Character Data (Quiz)
- How to Join Strings in Python (Quiz)
- How to Split a String in Python (Quiz)
- Formatting Python Strings (Course)
- A Guide to Modern Python String Formatting Tools (Quiz)
- Check if a Python String Contains a Substring (Course)
- How to Check if a Python String Contains a Substring (Quiz)
By Leodanis Pozo Ramos • Updated July 18, 2025