Python String Methods Flashcards
Converts the first character to upper case
capitalize()
Converts string into lower case
casefold()
Returns a centered string
center()
Returns the number of times a specified value occurs in a string
count()
Returns an encoded version of the string
encode()
Returns true if the string ends with the specified value
endswith()
Returns true if the string ends with the specified value
endswith()
Sets the tab size of the string
expandtabs()
Searches the string for a specified value and returns the position of where it was found
find()
Formats specified values in a string
format()
Formats specified values in a string
format_map()
Searches the string for a specified value and returns the position of where it was found
index()
Returns True if all characters in the string are alphanumeric
isalnum()
Returns True if all characters in the string are in the alphabet
isalpha()
Returns True if all characters in the string are ascii characters
isascii()
Returns True if all characters in the string are decimals
isdecimal()
Returns True if all characters in the string are digits
isdigit()
Returns True if the string is an identifier
isidentifier()
Returns True if all characters in the string are lower case
islower()
Returns True if all characters in the string are numeric
isnumeric()
Returns True if all characters in the string are numeric
isnumeric()
Returns True if all characters in the string are printable
isprintable()
Returns True if all characters in the string are whitespaces
isspace()
Returns True if the string follows the rules of a title
istitle()
Returns True if all characters in the string are upper case
isupper()
Converts the elements of an iterable into a string
join()
Converts the elements of an iterable into a string
join()
Returns a left justified version of the string
ljust()
Converts a string into lower case
lower()
Returns a left trim version of the string
lstrip()
Returns a translation table to be used in translations
maketrans()
Returns a tuple where the string is parted into three parts
partition()
Returns a string where a specified value is replaced with a specified value
replace()
Searches the string for a specified value and returns the last position of where it was found
rfind()
Searches the string for a specified value and returns the last position of where it was found
rindex()
Returns a right justified version of the string
rjust()
Returns a tuple where the string is parted into three parts
rpartition()
Splits the string at the specified separator, and returns a list
rsplit()
Returns a right trim version of the string
rstrip()
Splits the string at the specified separator, and returns a list
split()
Splits the string at line breaks and returns a list
splitlines()
Returns true if the string starts with the specified value
startswith()
Returns a trimmed version of the string
strip()
Swaps cases, lower case becomes upper case and vice versa
swapcase()
Converts the first character of each word to upper case
title()
Returns a translated string
translate()
Converts a string into upper case
upper()
Fills the string with a specified number of 0 values at the beginning
zfill()
Fills the string with a specified number of 0 values at the beginning
zfill()
How do string methods affect the string?
All string methods returns new values. They do not change the original string.
How do you assign strings to a variable?
- Assigning a string to a variable is done with the variable name followed by an equal sign and the string:
a = “Hello” - You can assign a multiline string to a variable by using three double or singe quotes:
a = “"”Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.””” - OR
a = ‘'’Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.’’’
How would you check if a character, word or phrase is in a string?
- To check if a certain phrase or character is NOT present in a string, we can use the keyword not in.
txt = “The best things in life are free!”
print(“expensive” not in txt) - OR
txt = “The best things in life are free!”
if “expensive” not in txt:
print(“No, ‘expensive’ is NOT present.”)