Python String Methods Flashcards
capitalize()
The capitalize() method returns a string where the first character is upper case.
str.capitalize()
txt = "hello, and welcome to my world." x = txt.**capitalize**()
casefold()
The casefold() method returns a string where all the characters are lower case.
string.casefold()
txt = "Hello, And Welcome To My World!" x = txt.**casefold**()
center()
The center() method will center align the string, using a specified character (space is default) as the fill character.
string.center(length, character)
txt = "banana" x = txt.**center**(20)
count()
The count() method returns the number of times a specified value appears in the string.
string.count(value, start, end)
txt = "I love apples, apple are my favorite fruit" x = txt.**count**("apple")
encode()
The encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used.
string.encode(encoding=encoding, errors=errors)
txt = "My name is Ståle" x = txt.**encode**()
endswith()
The endswith() method returns True if the string ends with the specified value, otherwise False.
string.endswith(value, start, end)
txt = "Hello, welcome to my world." x = txt.**endswith**(".")
expandtabs()
The expandtabs() method sets the tab size to the specified number of whitespaces.
string.expandtabs(tabsize)
txt = "H\te\tl\tl\to" x = txt.**expandtabs**(2)
find()
The find() method finds the first occurrence of the specified value.
The find() method returns -1 if the value is not found.
string.find(value, start, end)
txt = "Hello, welcome to my world." x = txt.**find**("welcome")
format()
The format() method formats the specified value(s) and insert them inside the string’s placeholder.
string.format(value1, value2…)
txt = "For only {price:.2f} dollars!" print(txt.**format**(price = 49))
format_map()
Formats specified values in a string
index()
The index() method finds the first occurrence of the specified value.
string.index(value, start, end)
txt = "Hello, welcome to my world." x = txt.**index**("welcome")
isalnum()
The isalnum() method returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9).
string.isalnum()
txt = "Company12" x = txt.**isalnum**()
isalpha()
The isalpha() method returns True if all the characters are alphabet letters (a-z).
string.isalpha()
txt = "CompanyX" x = txt.**isalpha**()
isdecimal()
The isdecimal() method returns True if all the characters are decimals (0-9).
string.isdecimal()
txt = "\u0033" #unicode for 3 x = txt.**isdecimal**()
isdigit()
The isdigit() method returns True if all the characters are digits, otherwise False.
string.isdigit()
txt = "50800" x = txt.**isdigit**()
isidentifier()
The isidentifier() method returns True if the string is a valid identifier, otherwise False.
string.isidentifier()
txt = "Demo" x = txt.**isidentifier**()
islower()
The islower() method returns True if all the characters are in lower case, otherwise False.
string.islower()
txt = "hello world!" x = txt.**islower**()