How to use string constants in Python

Created
Modified

String Constants in Python

The constants defined in this module are:

String constants:
string.ascii_letters
The concatenation of the ascii_lowercase and ascii_uppercase constants described below. This value is not locale-dependent.
string.ascii_lowercase
The lowercase letters 'abcdefghijklmnopqrstuvwxyz'. This value is not locale-dependent and will not change.
string.ascii_uppercase
The uppercase letters 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. This value is not locale-dependent and will not change.
string.digits
The string '0123456789'.
string.hexdigits
The string '0123456789abcdefABCDEF'.
string.octdigits
The string '01234567'.
string.punctuation
String of ASCII characters which are considered punctuation characters in the C locale: !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~.
string.printable
String of ASCII characters which are considered printable. This is a combination of digits, ascii_letters, punctuation, and whitespace.
string.whitespace
A string containing all ASCII characters that are considered whitespace. This includes the characters space, tab, linefeed, return, formfeed, and vertical tab.

Use of ascii_lowercase Constant

#!/usr/bin/env python3

import string

if 'a' in string.ascii_lowercase:
	print("Found!")

print(string.ascii_lowercase)
Found!
abcdefghijklmnopqrstuvwxyz

Use of string.digits Constant

#!/usr/bin/env python3

import string

for a in string.digits:
	print(a)
0
1
2
3
4
5
6
7
8
9

Related Tags