Saturday, October 21, 2023

Useful Python String Methods

import string
ascii_letters = list(string.ascii_letters)
print(ascii_letters)
ascii_lowercase = list(string.ascii_lowercase)
print(ascii_lowercase)
print(list(chr(x) for x in range(97, 123)))  
ascii_uppercase = list(string.ascii_uppercase)
print(ascii_uppercase)
print(list(chr(x) for x in range(65, 91))) 
digits = list(string.digits)
print(digits)
print(list(chr(x) for x in range(48, 58))) 
punctuation = list(string.punctuation)
print(string.punctuation)
print(punctuation)
# capitalize words
print(string.capwords("
ALL words iN sTRing's TransformeD 2to tiTle $CaSe# &caSe% ștuț şanţ ß groß ßa aßen"sep=None))

#Generate all possible pairs of 2 from the lower/upper case alphabet
from itertools import product
alphabet = list(string.ascii_letters)
two_letter_words = product(alphabet, alphabet)
for word in two_letter_words:
    print (word)

# Same result with repeat
two_letter_words = product(alphabet, repeat=2)
 
# print(len(two_letter_words))
#TypeError: object of type 'itertools.product' has no len()
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
['!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~']
All Words In String's Transformed 2to Title $case# &case% Ștuț Şanţ Ss Groß Ssa Aßen
('a', 'a')
('a', 'b')
('a', 'c')
('a', 'd')
...
('Z', 'X')
('Z', 'Y')
('Z', 'Z')

print(list(chr(x) for x in range(32, 48)))
[' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/']

newlist = [' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/']
print(list(ord(x) for x in newlist))[32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47]
*****

String constants

The constants defined in this module are:

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.

Helper functions

string.capwords(s, sep=None)
 
Split the argument into words using str.split(), capitalize each word using str.capitalize(), and join the capitalized words using str.join(). If the optional second argument sep is absent or None, runs of whitespace characters are replaced by a single space and leading and trailing whitespace are removed, otherwise sep is used to split and join the words.

 Source: https://docs.python.org