Python string Module
Example
Use string constants for validation:
import string
name = 'Linus123'
has_digit = any(char in string.digits for char in name)
print(f'Name contains digit: {has_digit}')
Try it Yourself »
Definition and Usage
The string module provides constants and classes for common string operations.
Use it to access predefined sets of characters (letters, digits, punctuation) or to create custom string formatters using the Template class.
Members
Member | Description |
---|---|
Formatter | Class for custom string formatting. |
Template | Class for simple string substitution with $-placeholders. |
ascii_letters | Concatenation of ascii_lowercase and ascii_uppercase. |
ascii_lowercase | Lowercase ASCII letters: 'abcdefghijklmnopqrstuvwxyz'. |
ascii_uppercase | Uppercase ASCII letters: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. |
capwords() | Split string into words and capitalize each word. |
digits | Decimal digits: '0123456789'. |
hexdigits | Hexadecimal digits: '0123456789abcdefABCDEF'. |
octdigits | Octal digits: '01234567'. |
printable | All printable ASCII characters. |
punctuation | ASCII punctuation characters. |
whitespace | All ASCII whitespace characters (space, tab, newline, etc.). |