String Methodz Flashcards
.capitalize()
Converts the first letter in string to uppercase.
.casefold()
Converts the entire string to lowercase.
.count()
Returns the number of times a specified value occurs in a string.
.count(value, start, end)
value = required
start = optional, default 0
end = optional, default end of string
.encode()
Returns an encoded version of the string, using specified encoding.
.encode(encoding=encoding, errors=errors)
encoding = optional, default UTF-8
errors = optional, specifies error handling (i.e. strict, ignore, backslashreplace)
.endswith()
Returns true if the string ends with the specified value.
.endswith(value, start, end)
value = string
start = optional (default 0)
end = optional (default end of string)
.find()
Searches the string for a specified value and returns the position of where it was found. Returns -1 if not found.
.find(value, start, end)
.format()
Formats specified values in a string; replaces placeholders defined with curly brackets.
.format(values)
.isalnum()
Returns True if all characters in the string are alphanumeric (a-z, 0-9).
.isalpha()
Returns True if all characters in the string are in the alphabet (a-z).
.isascii()
Returns True if all characters in the string are ASCII characters (a-z, 0-9, some special chars like $, &, *).
.isdecimal()
Returns True if all characters in the string are decimals (0-9).
.isdigit()
Returns True if all characters in the string are digits (0-9).
.isidentifier()
Returns True if the string is a valid identifier (a-z, 0-9 and _).
.islower()
Returns True if all characters in the string are lower case.
.isnumeric()
Returns True if all characters in the string are numeric.
.isprintable()
Returns True if all characters in the string are printable.
\n is not printable.
.isspace()
Returns True if all characters in the string are whitespaces.
.istitle()
Returns True if the string follows the rules of a title (all words start with an upper case letter AND the rest is lowercase).
Symbols and numbers ignored.
.isupper()
Returns True if all characters in the string are upper case.
.join()
Converts the elements of an iterable into a string, joined on separator.
separator.join(iterable)
“ “.join([“It’s”, “my”, “birthday”]) –> “It’s my birthday”
.lower()
Converts a string into lower case.
.partition()
Returns a tuple with three values based on the FIRST appearance of matched string.
.partition(value)
“I could eat bananas all day”.partition(“bananas”) –> (“I could eat”, “bananas”, “all day”)
If no match found for value, returns (str, ‘’, ‘’).
.replace()
Returns a string where a specified value is replaced with another specified value.
.replace(oldValue, newValue, count=ALL)
.rfind()
Returns the last occurrence of value, or -1 if not found.
.rfind(value)
.rindex()
Returns the last occurrence of value; raises ValueError if not found.
.rindex(value)
.rpartition()
Returns a tuple with three values based on the LAST appearance of matched string.
.partition(value)
“I could eat bananas all day”.partition(“bananas”) –> (“I could eat”, “bananas”, “all day”)
If no match found for value, returns (‘’, ‘’, str).
.rsplit()
Splits the string at the specified separator, and returns a list, starting from the right.
.rsplti(separator, maxSplit=-1 (ALL))
.split()
Splits the string at the specified separator, and returns a list.
.split(separator, maxSplit)
default = splits on whitespace.
.startswith()
Returns true if the string starts with the specified value.
.startswith(value, start, end)
.strip()
Returns a trimmed version of the string; trailing and leading spaces removed.
.strip(“str”) –> will also remove additional letters.
.swapcase()
Swaps cases; lower case –> uppercase AND uppercase –> lowercase.
.title()
Converts the first character of each word to upper case, rest to lowercase
.upper()
Converts a string into upper case.
.zfill()
Fills the string with leading zeroes until it reaches specified length.
.default 0
“potato”.zfill(8) –> “00potato”.