Python String Methods Explained with Examples
To apply a string method on a string.
Syntax: string.methodname()
Here are the string methods
- upper(): It is a string method in python it will convert all the lower case characters into uppercase and rest of characters remains same. It returns a new string.
2. lower(): It is a string method in python it will convert all the characters into lowercase and rest of characters remains same. It returns a new string.
3. swapcase(): It is a string method in python it will convert lowercase characters into uppercase and uppercase characters into lowercase. It returns a new string.
4. capitalize(): It is a string method in python,which will convert the first character into upper if it is a lowercase character rest of characters will be same. It returns a new string.
5. title(): It is a string method in python, which will convert the first character of a word into uppercase and rest of will be in lowercase. It returns a new string.
6. count(): It is a string method in python by using count we can count the number of times a substring is present inside a string. Count is always written an integer value.
count supports index, In this we use starting index and ending index. But in python it follows left to right only.
7. replace(): It is a string method in python it will replace a substring with another substring. It returns a new string. we can also control the limit.
8. index(): It is a string method in python, it is used to return the location of a substring occurring in the first occurrence of a string. It also supports starting and ending index values to specify the range in which Python should search for the substring.
9. rindex(): It is a string method in python, it is used to return the location of a substring occurring in the first occurrence of a string from right side.But index count starts from left to right only.
Get Vedasaraswathit’s stories in your inbox
Join Medium for free to get updates from this writer.
It also supports start and end index values to specify the range in which Python should search.
10. find(): It is a string method in python that is used to return the index of the first occurrence of a specified substring in a string.
It works similarly to the index() method. However, there is one important difference: if the substring is not found, find()returns -1 instead of raising an error.
11. rfind(): It is a string method in python that is used to return the index of the first occurrence of a specified substring in a string from right to left. But index count starts from left to right only.
12. strip(): It is a string method in python which will remove a characters/elements from the starting and ending of a string. It returns a new string.
13. lstrip(): It is a string method in python which will remove a characters/elements from the starting of a string. It returns a new string.
14. rstrip(): It is a string method in python which will remove a characters/elements from the ending of a string. It returns a new string.
15. spilt(): It is a string method in python which will split entire string into substring. Based on condition which we want split.
16. rsplit(): It is a string method in python which will split entire string into substring, from right to left.
17. partition(): It is a string method in python , it is same as split which will always split on substring.But here it follows a rule
i.e.,(“before split”, “split”, “after split”)
18. rpartition(): It is a string method in python , it is same as split which will always split on substring, from right to left.








