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