Python - String Methods Flashcards
Return - string
Args - none
___________
returns a string with first letter capitalized and all other characters lowercased. It doesn’t modify the original string
string.capitalize()
Return - string
Args - (width, ‘some char’)
___________
returns a string padded on both sides with specified char. It doesn’t modify the original string. If no second arg provided then blank spaces are default.
string.center()
Return - string
Args - none
___________
removes all case distinctions present in a string. It is used for caseless matching, i.e. ignores cases when comparing. For example, German lowercase letter ß is equivalent to ss. However, since ß is already lowercase, lower() method does nothing to it. But, casefold() converts it to ss.
string.casefold()
Return - int
Args - (substring, start, end)
___________
searches the substring in the given string and returns how many times the substring is present in it. It also takes optional parameters start and end to specify the starting and ending positions in the string respectively.
string.count()
Return - True/False
Args - (suffix, start, end)
___________
returns True if a string ends with the specified suffix. If not, it returns False.
string.endswith()
Return - copy of string
Args - (tab size)
___________
returns a copy of string with all tab characters ‘\t’ replaced with whitespace characters until the next multiple of tabsize parameter.
string.expandtabs()
Return - byte string
Args - (encoding=’UTF-8’,errors=’strict’)
___________
By default, encode() method doesn’t require any parameters.
It returns utf-8 encoded version of the string. In case of failure, it raises a UnicodeDecodeError exception.
However, it takes two parameters:
1) encoding - the encoding type a string has to be encoded to
2) errors - response when encoding fails.
There are six types of error response:
strict - default response which raises a UnicodeDecodeError exception on failure
ignore - ignores the unencodable unicode from the result
replace - replaces the unencodable unicode to a question mark ?
xmlcharrefreplace - inserts XML character reference instead of unencodable unicode
backslashreplace - inserts a \uNNNN espace sequence instead of unencodable unicode
namereplace - inserts a \N{…} escape sequence instead of unencodable unicode
string.encode()
Return - index
Args - (substring, start, end)
___________
returns the index of first occurrence of the substring (if found). If not found, it returns -1. Start and End args are optional.
string.find()
Return - formatted string with inputs
Args - (first input, second input, …)
___________
reads the type of arguments passed to it and formats it according to the format codes defined in the string. First value in given string is the argument it references and will substitute for in the given parameters, first number after colon is the number of total spaces allocated to the entire inputted argument, number after decimal with the f is the number of decimal places after the input number
“blah blah {0} blah blah {1:5.3f}”.format(‘input0’, ‘input2’)
Return - index
Args - (substring, start, end)
___________
returns the index of a substring inside the string (if found). If the substring is not found, it raises an exception
string.index()
Return - True/False
Args - none
___________
returns True if all characters in the string are alphanumeric (either alphabets or numbers). If not, it returns False
string.isalnum()
Return - True/False
Args - none
___________
returns True if all characters in the string are alphabets. If not, it returns False.
string.isalpha()
Return - True/False
Args - none
___________
returns True if all characters in a string are decimal characters. If not, it returns False.
string.isdecimal()
Return - True/False
Args - none
___________
returns True if all characters in a string are digits. If not, it returns False
string.isdigit()
Return - True/False
Args - none
___________
returns True if the string is a valid identifier in Python. If not, it returns False.
string.isidentifier()
Return - True/False
Args - none
___________
returns True if all alphabets in a string are lowercase alphabets. If the string contains at least one uppercase alphabet, it returns False.
string.islower()
Return - True/False
Args - none
___________
returns True if all characters in a string are numeric characters. If not, it returns False.
string.isnumeric()
Return - True/False
Args - none
___________
returns True if all characters in the string are printable or the string is empty. If not, it returns False.
Characters that occupies printing space on the screen are known as printable characters. For example:
letters and symbols
digits
punctuation
whitespace
string.isprintable()
Return - True/False
Args - none
___________
returns True if there are only whitespace characters in the string. If not, it return False.
Characters that are used for spacing are called whitespace characters. For example: tabs, spaces, newline etc.
string.isspace()
Return - True/False
Args - none
___________
returns True if the string is a titlecased string. If not, it returns False.
string.istitle()
Return - True/False
Args - none
___________
returns whether or not all characters in a string are uppercased or not.
string.isupper()
Return - concatenated string
Args - (iterable)
___________
provides a flexible way to concatenate string. It concatenates each element of an iterable (such as list, string and tuple) to the string and returns the concatenated string
separator = ‘, ‘
separator.join(someIterable)
Return - string
Args - (width, ‘optional fill char’)
___________
returns a left or right-justified string of a given minimum width.
string.ljust() and rjust()
Return - lowercased version of string
Args - none
___________
converts all uppercase characters in a string into lowercase characters and returns it.
string.lower()
Return - uppercased version of string
Args - none
___________
converts all lowercase characters in a string into uppercase characters and returns it.
string.upper()