Home »
Python
Python String Methods
By Shubham Singh Rajawat Last updated : December 1, 2023
Python has many built-in string methods to work with Python strings. In this tutorial, we will learn about the most popular string methods in Python.
1. Python String strip([chars]) Method
This method will return a string in which all the characters specified have been stripped of from the beginning and the end of the string.
Here,
- chars - characters to be removed from beginning or the end, by default space.
Example
Use of strip() method.
string="includehelp is a portal to learn concepts"
print(string)
print(string.strip('incspt')) #removes characters I, n, c, s, p, t from beg and end
print(string.strip()) #removes spaces from beg and end
Output
includehelp is a portal to learn concepts
ludehelp is a portal to learn conce
includehelp is a portal to learn concepts
2. Python String find(str,beg,end) Method
This method returns the position of the first occurrence of specified string between 'beg' and 'end' where beg is inclusive and end is exclusive. If the string is not found returns -1.
Here,
- str - string to be searched
- beg - starting index, by default 0
- end - ending index, by default length of string
Example
Use of find() method.
string="includehelp is a portal to learn concepts"
print(string.find("include",0,6)) #return -1 as 6 is exclusive
print(string.find("include",0,7))
print(string.find("por"))
Output
-1
0
17
3. Python String rfind(str,beg,end) Method
This method returns the position of the last occurrence of the specified string. This function works similar to 'find()'.
4. Python String split(str,limit) Method
As name specifies it splits the string from the specified delimeter 'str', this method returns a list.
Here,
- str - this is any delimiter, by default space
- limit - limits numbers to split
Example
Use of split() method.
string="includehelp is a portal to learn concepts"
l=string.split()
print(l)
m=string.split('o')
print(m)
n=string.split('o',1)
print(n)
Output
['includehelp', 'is', 'a', 'portal', 'to', 'learn', 'concepts']
['includehelp is a p', 'rtal t', ' learn c', 'ncepts']
['includehelp is a p', 'rtal to learn concepts']
5. Python String lower() Method
This method returns a string with all the letters in lowercase.
6. Python String upper() Method
This method returns a string with all letters in uppercase.
7. Python String title() Method
This method returns a string by converting it into title case i.e. first letter of all the words will be uppercase.
8. Python String capitalize() Method
This method returns a string with only the first letter of the first word as uppercase.
9. Python String startswith(prefix,beg,end) Method
This method returns true if the string starts with specified string 'prefix' else return false.
Here,
- prefix - string to be checked
- beg - starting index, by default 0
- end - ending index ,optional, by default length of string
10. Python String endswith(suffix,beg,end) Method
This method returns true if the string ends with specified string 'suffix' else return false.
Here,
- suffix - string to be checked
- beg - starting index, by default 0
- end - ending index ,optional, by default length of string
Example
Program to use above methods.
string="Includehelp is a portal to Learn Concepts"
print(string.lower())
print(string.upper())
print(string.title())
print(string.capitalize())
print(string.endswith('rn',0,32))
print(string.startswith('rt',19))
Output
includehelp is a portal to learn concepts
INCLUDEHELP IS A PORTAL TO LEARN CONCEPTS
Includehelp Is A Portal To Learn Concepts
Includehelp is a portal to learn concepts
True
True
11. Python String len(str) Method
This function will return the length of the string(str) given as argument.
12. Python String lstrip([chars]) Method
This method will remove all the characters specified from the left of the string.
Here,
chars it contains all the characters, by default space.
13. Python String rstrip([chars]) Method
This method will remove all the characters specified from the right of the string.
Here,
chars it contains all the characters, by default space.
Example
Program to use above methods.
string="Includehelp is a portal to Learn Concepts"
print('Length of string is',len(string))
print(string.lstrip('Isctn'))
print(string.rstrip('Instpc'))
Output
Length of string is 41
ludehelp is a portal to Learn Concepts
Includehelp is a portal to Learn Conce
14. Python String str.join(sequence) Method
This method will return a string, which is the concatenation of the strings in 'sequence' by the separator 'str'.
Here,
sequence sequence- this the sequence of elements to be joined.
Example
Program to use join() method.
str=' '
l=('Include','help','is','a','portal','to','learn')
print(str.join(l))
str='_'
l=('Include','help')
print(str.join(l))
Output
Include help is a portal to learn
Include_help
15. Python String isalpha() Method
This method returns true if string only contains alphabets else return false.
16. Python String isalnum() Method
This method returns true if the string is combination of alphabets and numbers only else return false.
Example
Program to use above methods.
string="Includehelp"
string1="Include help"
string2='123456'
string3='12345Hii'
print(string.isalpha())
print(string1.isalpha()) #It will return False as string1 contains a space
print(string2.isalnum())
print(string3.isalnum())
Output
True
False
True
True
17. Python String count(str,beg,end) Method
This method will count the occurrence of the substring between specified indices.
Here,
- str - substring to be count.
- beg - it is the beginning index, by default 0.
- end - it is the end index, by default string length.
Example
Program to use count() method.
string="Includehelp is a portal to Learn Concepts"
print(string.count('a'))
Output
3