Python String Manipulation and Graphs Flashcards
What is a raw string?
Suppresses blackslashes in string.
Make one by putting r before first quote e.g. r”some text”
It’s type is str just like regular strings
What is __doc__?
-If the first thing in a function or class body is a string literal, it is stored in the \_\_doc\_\_ attribute of the function/class -Can also be unicode strings
What data type does python not have?
character
Get the length of a string
– len(string)
Access a character in the string with subscript notation
– c = string[1]
● Access a substring of a string with slice notation
– sub = string[1:5]
● Reverse a string by using slice notation with a negative step
– rev = string[::-1]
● Iterate over the characters of a string
– for c in string:
Convert a string to an array of strings split on a given
separator
– arr = string.split(separator)
Concatenate an array of strings into a single string, inserting a separator between each
– s = separator.join(Iterable)
Standard approach to build a String Character-by-Character
The standard approach is to build an array of singlecharacter strings, then use join()
What are the string predicates?
Test type of characters in string; return true if nonempty and all characters are of the specified type
How do you capitalize the first character in a string?
cap = string.capitalize() first char uppercased, rest lowercased
How do you uppercase->lowercase and vice versa?
– swp = string.swapcase()
How do you capitalize the first character in every word?
– tc = string.title()
How to check if a substring is a prefix
b = string.startswith(sub)
How to check if a substring is a suffix
b = string.endswith(sub)
How to get the index of a substring (2)
– idx = string.find(sub) index of first occurrence of sub or -1
– idx = string.rfind(sub) index of last occurrence of sub or -1
How to count number of occurences of substring in a string
string.count(sub) (can’t be overlapping)
Replace occurrences of a substring with a new string with and without a limit number (2)
– rep = string.replace(old,new) all occurrences
– rep = string.replace(old,new,count) at most count occur
Partition string around a separator (2)
– (before, sep, after) = string.partition(sep) use first occur of sep
– (before, sep, after) = string.rpartition(sep) use last occur of sep
The separator can be a regex!
add spaces to center string in width
– j = string.center(width)
add spaces to start/end as needed
– j = string.[lr]just(width)
add leading zeros to number as needed
– n = string.zfill(width)
Remove leading and trailing spaces from string (3)
– s = string.strip() remove leading and trailing whitespace
– s = string.lstrip() remove leading whitespace
– s = string.rstrip() remove trailing whitespace
use conversion specifier with format function
“Hello, {conversion_specifier}”.format(some_number)
Conversion specifier for 3 digit zero padded decimal
%03d
For f string, when is value evaluated?
value is evaluated at run-time
What are the magic functions for string formatting? (3)
-__str__ is called by print() to convert an object to a readable string
– __repr__ converts an object into an unambiguous string representation; it is called by the default __str__() for containers, by print() if __str__ is not defined, and by str.format() and f-strings if the !r conversion specifier is given
– __format__ is called by str.format() and f-strings if formatting options are present
Check if string contains only letters or digits
– b = string.isalnum()
Check if string contains only characters valid for forming a number
– b = string.isnumeric()
Check if string contains only characters valid for a radix-10 number
– b = string.isdecimal()
Check if string contains only letter (uppercase or lowercase)
– b = string.isalpha()
Check if string contains only lowercase letter
– b = string.islower()
Check if string contains only uppercase letter
– b = string.isupper()
Check if string contains only digits
– b = string.isdigit()
Check if string contains only whitespace
– b = string.isspace()
What type do you use for regex in python?
raw strings