Strings Flashcards
functions and methods for string Manipulation
string.ascii_letters
A constant, all the ASCII letters:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
string.ascii_lowercase
A constant, all the lowercase ASCII letters:"abcdefghijklmnopqrstuvwxyz"
string.ascii_uppercase
A constant, all the uppercase ASCII letters:"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
string.digits
A constant, the digits:"0123456789"
string.hexdigits
A constant, the digits plus uppercase and lowercase letters a-f for hexadecimal:"0123456789abcdefABCDEF"
string.octdigits
A constant. The digits for octal:"01234567"
string.printable
A constant string. All the ASCII symbols considered printable. Letters, numbers, grammer, whitespace.
string.punctuation
A constant. Punctuation symbols
"%&'()*+,-./:;<=>?@[\]^_\`{|}~"
string.whitespace
A constant. All the whitespace characters: Space, tab, linefeed, return, formfeed, vertical tab
textwrap.fill(my_paragraph, 50)
Adds newline characters to my_paragraph
such that each line is no more than 50. This left aligns the text
textwrap.dedent(my_paragraph)
Remove any leading whitespace common to all lines in my_paragraph
textwrap.dedent("\t\t hello\n\t world) >>> "\t hello\n world"
textwrap.indent(my_text, "\t")
Indent all lines in my_text
with \t
textwrap.indent(my_text, "\t", lambda line: "cats" in line)
indent all lines with “\t” in my_text
which contain the string "cats"
textwrap.shorten(“Hello world, my name is Philip”, 12)
"hello [...]"
shortens the text to width 12. Adds in [...]
placeholder to indicate it is shortened. Can use a custom placeholder with the placeholder
argument.
re.search(my_regex, my_text)
Finds the first occurrance of the pattern specified in my_regex
in my_text
. Returns None
when no match is present, or a re.Match
object when there is a match.