Python String Methods
Python String maketrans() Method
Returns a translation table to be used in translations. Note that this doesn't do the translation,
but rather creates the map for use in the translate
function.
Python String partition() Method
Searches for the specified substring to match on and returns a tuple of three strings: everything before the match, the match string, everything after the match string.
Python String replace() Method
Replaces all occurrences of the substring with a new string. The number of occurrences can also be specified.
Python String rfind() Method
Returns the index of the last occurrence found of the specified substring. Python will return -1 if the search string is not found.
Python String rindex() Method
Returns the index of the last occurrence found of the specified substring. Python will
throw a ValueError
exception if the substring is not found.
Python String rjust() Method
Right justifies a string, left-padding with the specified character or substring to the specified width.
Python String rpartition() Method
Searches the source string from the right for the first match of the specified substring and returns a tuple with everything to the left of the match (from the right), the match, and everything to the right of the first match (again, from the right.)
Python String rsplit() Method
Splits the string by the given separator, from right to left. An array is returned for the separated strings. And optional number of occurrences can be specified.
Python String rstrip() Method
Removes the specified set of characters from the end of the source string.
Python String split() Method
Splits the string by the given separator, from left to right. An array is returned for the separated strings. An optional number of occurrences can be specified.
Python String splitlines() Method
Splits a line by carriage returns and/or line feed characaters. The return is an array of strings representing separate lines.
Python String lstrip() Method
Removes the specified leading characters from the start of a string.
Python String startswith() Method
Returns True if the string starts with the specified substring. This is a case sensitive test.
Python String swapcase() Method
Python String title() Method
Converts the first character of each word to uppercase and the rest of the characters in each word to lowercase, such that the resulting string conforms with the rules for a title.
Python String translate() Method
Given a string, translate replaces characters in that string with other characters as specified in the supplied "translation" and returns the new string. A translation is simply a map of characters, and the characters to those characters with.
Python String upper() Method
Converts the letters a-z to uppercase.
Python String zfill() Method
Fills the string with a specified number of 0 values at the beginning
Python String strip() Method
Removes the specified set of characters from the start and end of the source string.
Python String lower() Method
Converts a string to all lowercase characters:
Python String join() Method
Joins an array of strings with the specified string.
Python String capitalize() Method
This function capitalizes the first character in a string. The character must be a letter a through z.
Python String casefold() Method
Converts all letters A through Z to lowercase.
Python String center() Method
Centers a string to fit in the specified width, with an optional fill character. If the string is longer than the specified width, the string is returned without being centered.
Python String count() Method
Returns the number of times a specific character or string appears in another string. The count
function can take an optional start
and end index into the string.
Python String encode() Method
Returns an encoded version of a string.
Python String endswith() Method
Returns true if the string ends with the specified character or substring. The comparison is case-sensitive.
Python String expandtabs() Method
Replaces tab characters in a string with the specified number of whitespaces.
Python String find() Method
Searches the string for a specified value and returns the index of the first occurrence of the specified substring. Optional start and end indices can also be specified.
Python String format() Method
The format
method formats the specified values and inserts them inside the string's placeholder. The format
method requires comma delimited parameters specifying the placeholder name and the placeholder's value.
Python String ljust() Method
Returns a left-justified version of the string with the specified width and fill character.
Python String format_map() Method
The format_map
formats a string by taking a dictionary of key-value pairs that define the placeholder names and their values, and replacing the placeholders in a string with the values.
Python String isalnum() Method
Returns True if all the characters in the string are alphanumeric. This would be a-z, A-Z, and 0-9. Punctuation, spaces, symbols, and other characters are not considered a letter or a number.
Python String isalpha() Method
Returns True if all the characters in the string are alphabetic. This would be a-z or A-Z.
Python String isdecimal() Method
Returns True if all characters in the string are decimals, which includes decimal characters in various languages and Unicode characters that are digits.
Python String isdigit() Method
Returns True if all characters in the string are digits. This includes superscript / subscript digits and digits in other languages.
Python String isidentifier() Method
Returns True if the string is considered to be an identifier. An identifier can only contain the lowercase letters a-z, A-Z, digits 0-9, and underscores.
Python String islower() Method
Returns True if all characters in the string are lowercase. Symbols and numbers are ignored.
Python String isnumeric() Method
Returns True if all characters in the string are digits. This includes superscript / subscript digits, fraction characters and digits in other languages.
Python String isprintable() Method
Returns True if the all characters in a string are printable. Examples of non-printable characters are carriage return, line feed, and tab.
Python String isspace() Method
Returns True if all characters in the string are whitespace. Tabs, carriage returns and line feeds are considered to be whitespace.
Python String istitle() Method
Returns True if the string is considered a title. A title is where all words begin with an uppercase letter and the rest of the characters in the word are lowercase. Symbols and numbers are ignored.
Python String isupper() Method
Returns true if all characters in the string are uppercase. Symbols and numbers are ignored.
Python String index() Method
Returns the index of the first matching substring within the given string. If the index is not found, Python throws a ValueError
exception.