| Action |
Code |
Details |
|
Lowercase and uppercase letters
|
|
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ |
|
Lowercase letters
|
|
abcdefghijklmnopqrstuvwxyz |
|
Uppercase letters
|
|
ABCDEFGHIJKLMNOPQRSTUVWXYZ |
|
Digits
|
|
0123456789 |
|
Hexadecimal digits
|
|
0123456789abcdefABCDEF |
|
Whitespace characters
|
|
Includes space, tab, linefeed, return, formfeed, and vertical tab. |
|
Punctuation characters
|
|
!"#$%&'()*+,-./:;<=>?@[]^_`{|}~ |
|
Printable characters
|
|
Combination of digits, letters, punctuation, and whitespace. |
| Action |
Code |
Details |
|
Empty
|
|
|
|
Literal
|
|
|
|
Literals (concatenate)
|
|
|
|
Random lowercase string of length n
|
''.join(random.choices(string.ascii_lowercase + string.digits, k=n))
|
|
|
Random alphanumeric string of length n
|
''.join(random.choices(string.ascii_lowercase + string.digits, k=n))
|
|
|
From list, separated by comma
|
|
|
|
Object to string
|
|
|
|
Positional formatting
|
'First {0} then {1}'.format(1 + 1, 2 * 2)
|
|
|
Named formatting
|
'First {sum} then {mult}'.format(sum = 1 + 1, mult = 2 * 2)
|
|
|
Named element formatting
|
'a0 = {a[0]}'.format(a=[1,2])
|
|
|
Named attribute formatting
|
'Instance is of type: {p.type}'.format(p=Prop)
|
|
|
Named formatting of whole number
|
'a = '{num:,}'.format(num = int_var)
|
|
|
Dynamic formatting based on dict
|
'Value of a and b is {a} and {b}'.format_map(dict(a=1, b=2))
|
|
|
Whole number
|
|
|
|
Whole number with thousands separator
|
|
Outputs '1,000' |
|
Padded whole number
|
|
Outputs ' 3' |
|
Zero-padded whole number
|
|
Outputs '003' |
|
Float
|
|
|
|
Float as whole number
|
'a = {:.0f}'.format(3.14)
|
Outputs '3' |
|
Float with decimal-point padding
|
'a = {:06.2f}'.format(3.1234)
|
|
|
Datetime with format
|
'{:%Y-%m-%d %H:%M}'.format(datetime(2001, 2, 3, 4, 5))
|
|
| Action |
Code |
Details |
|
Split string into two parts by separator (as triplet)
|
|
|
|
Split string into multiple parts by separator sep (as list)
|
|
|
|
Split string into lines (as list)
|
|
|
|
Split string into cumulative parts by separator sep (as list)
|
list(itertools.accumulate(x.split(sep), lambda x, y: sep.join([x, y])))
|
For sep='.', a.b.c becomes [a, a.b, a.b.c] |