Lecture 2.6 - String Methods
Lecture 2.6 - String Methods
String methods 1
Method Description Code Output
islower() Returns True if all characters x = 'python' True
in the string are lower case print(x.islower())
x = 'Python' False
print(x.islower())
isupper() Returns True if all characters x = 'PYTHON' True
in the string are upper case print(x.isupper())
x = 'PYTHoN' False
print(x.isupper())
istitle() Returns True if the string x = 'Pyhton String Methods' True
follows the rules of a title print(x.istitle())
x = 'Pyhton string methods' False
print(x.istitle())
String methods 2
Method Description Code Output
isdigit() Returns True if all characters x = '123' True
in the string are digits print(x.isdigit())
x = '123abc' False
print(x.isdigit())
isalpha() Returns True if all characters x = 'abc' True
in the string are in alphabets print(x.isalpha())
x = 'abc123' False
print(x.isalpha())
isalnum() Returns True if all characters x = 'abc123' True
in the string are alpha-numeric print(x.isalnum())
x = 'abc123@*#' False
print(x.isalnum())
String methods 3
Method Description Code Output
x = '-----Python-----'
strip() Returns a trimmed version of print(x.strip('-')) Python
the string
lstrip() Returns a left trim version of print(x.lstrip('-')) Python-----
the string
rstrip() Returns a right trim version of print(x.rstrip('-')) -----Python
the string
String methods 4
Method Description Code Output
x = 'Python'
startswith() Returns True if the string starts print(x.startswith('P')) True
with the specified value print(x.startswith('p')) False
endswith() Returns True if the string ends print(x.endswith('n')) True
with the specified value print(x.endswith(‘N')) False
String methods 5
Method Description Code Output
x = 'Python String Methods'
count() Returns the number of times a print(x.count('t')) 3
specified value occurs in a
string print(x.count(‘s')) 1
index() Searches the string for a print(x.index('t')) 2
specified value and returns the
position of where it was found print(x.index(‘s')) 20
replace() Returns a string where a x = x.replace('S', 's') Python string methods
specified value is replaced x = x.replace('M', 'm')
with a specified value print(x)
String methods 6