String Function in Python - Python String Methods
Hello Everybody, In this article we will be discussing over built-in Python String functions. you might be familiar with some of these methods but we will also be looking through some rare ones which are useful.
So, let's start covering one by one learning about them and also we will be going through one or more examples for each method so that you can get the clarity about the working of each function.
String Functions in Python
1. Capitalize - It converts the first character of the string to Capital letter.
s = "iconic python"
s.capitalize()
Iconic python
2. Upper - It converts all the characters to uppercase.
s = "iconic python"
s.upper()
ICONIC PYTHON
3. Lower - It converts all the characters to lowercase.
s = "ICONIC PYTHON"
s.lower()
iconic python
4. Isupper - It checks if all the characters are uppercase.
s = "ICONIC PYTHON"
s.isupper()
True
5. Islower - It checks if all the characters are lowercase.
s = "Iconic Python"
s.islower()
False
6. Isalpha - It checks if all the characters are alphabet.
s = "Iconic Python"
s.isalpha()
False
Note - In above example String contains a Space and that's why it returns false as space is a special character not an alphabet.
7. Isdigit - It checks and returns True if all the characters are digits.
s = "Iconic Python"
s.isdigit()
False
8. Isalnum - It checks if all the characters are alphanumeric (Letters and Numbers).
s = "IconicPython123"
s.isalnum()
True
9. Count - It counts the number of occurrences of a given character in a String.
s = "Iconic Python"
s.count("c")
2
10. Find - It returns the index of the first occurrence of the given character in a string. You can also pass a substring, then it will return you the index of that character from which the substring starts.
s = "Iconic Python"
s.find("c")
1
s = "Iconic Python"
s.find("Py")
7
If you want to find the second or any other occurrence of the character, then pass a second parameter equal to the which occurrence of the character you want to print.
s = "Iconic Python"
s.find("c",2)
5
11. Startswith - It checks if the string starts with the specified string.
s = "Iconic Python"
s.startswith("I")
True
12. Endswith - It checks if the string ends with the given character.
s = "Iconic Python"
s.endswith("n")
True
13. Replace - It replaces the string or a sub string with the given string.
s = "Iconic Python"
s.replace("Iconic","Strings in")
Strings in Python
14. Split - It splits the string at the occurrence of the given character or substring and returns a list containing each part of the string after splitting.
s = "Iconic Python"
s.split("c")
['I', 'oni', ' Python']
By default it splits the string at whitespace.
s = "Iconic Python"
s.split()
['Iconic', ' Python']
15. Swapcase - It converts all uppercase to lowercase and lowercase to uppercase.
s = "Iconic Python"
s.swapcase()
iCONIC pYTHON
These are some widely used string methods. I hope you like the article and the information shared in this may proven helpful to you. Share this article with your friends and on your social media handles so that more programmers and coding beginners can take advantage of the content.
Also Read :-
Extract Phone Number Details Using Python
Post a Comment
For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.