String methods & built in functions
Method Result Example
len() Returns the length of the string r=len(a)
will be 4
str.capitalize() To capitalize the first character of the r=a.capitalize()
string/sentence and rest of it is in lower will be “Comp”
case
str.title() Will return title case string r=b.title()
where first letter of each word is in will be
upper case. My Comp
str.upper() Will return string in upper case r=a.upper()
will be “COMP”
str.lower() Will return string in lower case r=c.lower()
will be “comp”
will return the total count of a given
str.count() element in a string r=a.count(‘o’)
will be 1
To find the starting index value of r=a.find (‘m’)
str.find(sub) the substring position(starts from will be 2
0 index)
Returns the string with replaced r=b.replace(‘my’,’yo
str.replace() sub strings ur’) will be
‘your comp’
r=a.index(‘om’)
str.index() Returns index position of substring will be 1
String consists of only alphanumeric r=c.isalnum()
str.isalnum() characters (no symbols) returns True
returns True if String consists of r=a.isalpha()
str.isalpha() only alphabetic characters (no returns True
symbols)
str.islower() returns True if String’s alphabetic r=a.islower()
characters are all lower case returns True
str.isdigit() returns True if String consists of only r=b.isdigit()
numeric characters returns True
str.isspace() returns True if String consists of only r=d.isspace()
whitespace characters returns True
str.isupper() returns True if String’s alphabetic r= a.isupper()
characters are all upper case returns false
b=‘**comp’;
str.lstrip(char) Returns a copy of the string with r=b.lstrip(‘*’)
str.rstrip(char) leading/trailing characters/spaces will be
removed ‘comp’
Removes specific character/spaces b=‘@@comp@@’;
str.strip(char) from leading and trailing position r=b.lstrip(‘@’)
will be
‘comp’
b=‘my comp’;
r=b.split() will be
str.split() Returns list of strings as splitted
[‘my’,‘comp’]
a=('jan', 'feb','mar')
b=‟&”
str.join () Returns a string in which the string a.join(b)
elements have been joined by a 'jan&feb&mar'
separator.
partition(sep) The function partitions the strings at the a='The Green
first occurrence of separator, and returns Revolution'
the strings partition in three parts i.e. a.partition('Rev')
before the separator, the separator itself, ('The Green ', 'Rev',
and the part after the separator.(in form 'olution')
of a tuple).
str.partition('pe')
If the separator is not found, returns the ('The Green
string itself, followed by two empty Revolution', '', '')
strings