Strings Methods and Built-in Functions
No Method Description Example
1 len() Returns the length of the given string >>> str1 = 'Hello World!'
>>> len(str1)
12
2 capitalize() Returns the string with its first >>>'hello world Hai'.capitalize( )
character capitalized and the rest in 'Hello world hai'
lowercase.
3 title() Returns the string with first letter of >>>'My name is RAJ'.title( )
every word in the string in uppercase 'My Name Is Raj'
and rest in lowercase.
4 upper() Returns the string with all lowercase >>> str1 = 'hello WORLD!'
letters converted to uppercase. >>> str1.upper()
'HELLO WORLD!'
5 lower() Returns the string with all uppercase >>> str1 = 'hello WORLD!'
letters converted to lowercase. >>> str1.lower()
'hello world!'
6 count(str, start, end) Returns number of times substring str >>> str1 = 'Hello World! Hello'
occurs in the given string. If we do >>> str1.count('Hello',12,25)
not give start index and end index 1
then searching starts from index 0 and >>> str1.count('Hello')
ends at length of the string 2
7 find(str,start, end) Returns the first occurrence of index >>> str1 = 'Hello World! Hello'
of substring str occurring in the given >>> str1.find('Hello',10,20)
string. 13
If we do not give start and end then >>> str1.find('Hello',15,25)
searching starts from index 0 and ends -1
at length of the string. >>> str1.find('Hello')
If the substring is not present in the 0
given string, then the function returns >>> str1.find('Hee')
-1 -1
8 index(str, start, end) Same as find() but raises an exception >>> str1 = 'Hello World! Hello'
if the substring is not present in the >>> str1.index('Hello')
given string 0
>>> str1.index('Hee')
ValueError: substring not found
9 endswith() Returns True if the given string ends >>> str1 = 'Hello World!'
with the supplied substring otherwise >>> str1.endswith('World!')
returns False . True
>>> str1.endswith('!')
True
>>> str1.endswith('lde')
False
10 startswith() Returns True if the given string starts >>> str1 = 'Hello World!'
with the supplied substring otherwise >>> str1.startswith('He')
returns False True
>>> str1.startswith('Hee')
False
Maya Giby
PGT Computer Sc
11 isalnum( ) Returns True if the characters in the >>>'abc123'.isalnum( )
string are alphabets or numbers and True
there is at least one character, False >>>'hello'.isalnum( )
otherwise. True
>>>'123'.isalnum( )
True
>>>' '.isalnum( )
False
>>>'abc 123'.isalnum()
False
>>>'abc###123'.isalnum()
False
12 isalpha() Returns True if the characters in the >>>'abc123'.isalpha( )
string are alphabets and there is at False
least one character, False otherwise. >>>'hello'.isalpha( )
True
>>>'123'.isalpha( )
False
>>>' '.isalpha( )
False
13 isdigit() Returns True if the characters in the >>>“abc123”.isdigit( )
string are digits and there is at least False
one character, False otherwise. >>>“hello”.isdigit( )
False
>>>“123”.isdigit( )
True
>>>“ “ .isdigit( )
False
14 islower( ) Returns True if all cased characters in >>>“hello”.islower( )
the string are lowercase and there True
must be at least one cased character. It >>>“THERE”.islower( )
returns False otherwise. False
>>>“Goldy”.islower( )
False
15 isupper( ) Returns True if all cased characters in >>>“hello”.isupper( )
the string are uppercase and there False
must be at least one cased character. It >>>“THERE”.isupper( )
returns False otherwise. True
>>>“Goldy”.isupper( )
False
>>>“U123”.isupper( )
True
>>>“123f”.isupper( )
False
16 isspace ( ) Returns True if there are only >>>“ “.isspace( )
whitespace characters in the string. True
There must be at least one character. >>>“”.isspace( )
It returns False otherwise. False
17 lstrip() Returns the string after removing the >>> str1 = ' Hello World!'
spaces only on the left of the string. >>> str1.lstrip()
'Hello World!
18 rstrip() Returns the string after removing the >>> str1 = 'Hello World! '
spaces only on the right of the string. >>> str1.rstrip()
'Hello World!'
Maya Giby
PGT Computer Sc
19 strip() Returns the string after removing the >>> str1 = ' Hello World! '
spaces both on the left and the right of >>> str1.strip()
the string. 'Hello World!'
20 replace(oldstr, newstr) Replaces all occurrences of old string >>> str1 = 'Hello World!'
with the new string. >>> str1.replace('World','Country')
'Hello Country!'
>>> str1 = 'Hello World! Hello'
>>> str1.replace('Hello','Bye')
'Bye World! Bye'
21 join() Returns a string in which the >>> str1 = 'HelloWorld!
characters in the string have been >>> str2 = '-' #separator
joined by a separator. >>> str2.join(str1)
'H-e-l-l-o-W-o-r-l-d-!'
22 partition() Partitions the given string at the first >>> str1 = 'India is a Great Country'
occurrence of the substring (separator) >>> str1.partition('is')
and returns the string partitioned into ('India ', 'is', ' a Great Country')
three parts as a tuple. >>> str1.partition('are')
1. Substring before the separator ('India is a Great Country', ' ' , ' ')
2. Separator
3. Substring after the separator
If the separator is not found in the
string, it returns the whole string itself
and two empty strings
23 split() Returns a list of words delimited by >>> str1 = 'India is a Great Country'
the specified substring. If no delimiter >>> str1.split()
is given then words are separated by ['India','is','a','Great', 'Country']
space. >>> str1 = 'India is a Great Country'
>>> str1.split('a')
['Indi', ' is ', ' Gre', 't Country']
>>>str1.split('b')
['India is a Great Country']
Maya Giby
PGT Computer Sc