String Methods Flashcards
.capitalize()
(string method)
Converts the first character to upper case
.casefold()
(string method)
Converts all the uppercase letters in a string to lowercase letters
.count(substr)
(string method)
counts the number of times the substr appears in string
.endswith(substr)
(string method)
Returns true if the string ends with the substr, else False
.find(substr)
(string method)
Returns the index of the first occurence of the substr in the string
Returns -1 if substr not found in string
.format()
example
“My name is {}, I’m {}”.format(“John”,36)
“My name is {0}, I’m {1}”.format(“John”,36)
“My name is {fname}, I’m {age}”.format(fname = “John”, age = 36)
(string method)
The format() method formats the specified value(s) and insert them inside the string’s placeholder.
The placeholder is defined using curly brackets: {}
.index(substr)
(string method)
returns the index of the specified substr in the str
raises an exception if the value is not found
(very similar to .find(), but .find() return -1 when the substr is not found)
.isalnum()
(string method)
returns True if all characters are alphanumeric
.isalpha()
(string method)
Returns True if all characters in the string are in the alphabet
.isascii()
(string method)
Returns True if all characters in the string are ascii characters
.isdigit()
(string method)
Returns True if all characters in the string are digits
.lower()
(string method)
Returns the lowercased value of the input string
.replace(old, new, count)
(string method)
returns a copy of the string where the old substring is replaced with the new string
params
1. old = value to replace
2. new = what to replace it with
3. (optional) # of instances (defaults to replacing ALL)
.upper()
(string method)
method returns a string where all characters are in upper case.
.strip()
(string method)
Returns a trimmed version of the string (no trailing or leading spaces)