String Methods in Python
Ankit Kumar
Posted on May 10, 2023
An overview of string methods in Python
In this article, I will provide an overview of the most commonly used string methods in Python.
1. capitalize()
Converts the first character of the string to a capital (uppercase) letter.
example:-
my_string = "ankit kumar"
print(my_string.capitalize()) : Ankit kumar
2. center()
Pad the string with the specified character.
example:-
my_string= "ankit"
print(my_string.center(11,'_')) : ___ankit___
3. count()
Returns the number of occurrences of a substring in the string.
example:-
my_string= "ankitki"
print(my_string.count("ki")) : 2
4. endswith()
Returns “True” if a string ends with the given suffix else return "False"
example:-
my_string= "ankit"
print(my_string.endswith("t")) : True
print(my_string.endswith("t",1,2)) : False
5. expandstabs()
Specifies the amount of space to be substituted with the “\t” symbol in the string
example:-
my_string= "ankit\tkumar"
print(my_string.expandtabs()) : ankit kumar
6. find()
Returns the lowest index of the substring if it is found.
example:-
my_string= "ankitkumar"
print(my_string.find("ku")) : 5
7. format()
Formats the string for printing it to console
example:-
name = "Ankit"
age = 25
print("My name is {}, and I'm {} years old.".format(name, age))
Output: "My name is Ankit, and I'm 25 years old."
8. index()
Returns the position of the first occurrence of a substring in a string.
example:-
sentence = "The quick brown fox jumps over the lazy dog"
index = sentence.index("fox")
print(index) # Output: 16
9. isalnum()
Checks whether all the characters in a given string is alphanumeric or not
example:-
string1 = "hello123"
string2 = "hello 123"
print(string1.isalnum()) # Output: True
print(string2.isalnum()) # Output: False
10. isalpha()
Returns “True” if all characters in the string are alphabets
example :-
string1 = "hello"
string2 = "hello123"
print(string1.isalpha()) # Output: True
print(string2.isalpha()) # Output: False
11. isdigit()
Returns “True” if all characters in the string are digits.
example:-
string1 = "12345"
string2 = "12.34"
print(string1.isdigit()) # Output: True
print(string2.isdigit()) # Output: False
12. islower()
Checks if all characters in the string are lowercase.
example:-
string1 = "12345"
string2 = "12.34"
print(string1.isdigit()) # Output: True
print(string2.isdigit()) # Output: False
13. isnumeric()
Returns “True” if all characters in the string are numeric characters.
example:-
string1 = "12345"
print(string1.isnumeric()) # Output: True
14. isprintable()
Returns “True” if all characters in the string are printable or the string is empty.
example:-
string1 = "Hello World!"
string2 = "Hello\nWorld!"
print(string1.isprintable()) # Output: True
print(string2.isprintable()) # Output: False
15. title()
Convert string to title case.
example:-
string = "this is a test"
new_string = string.title()
print(string) # Output: this is a test
print(new_string) # Output: This Is A Test
16. upper()
Converts all lowercase characters in a string into uppercase.
example:-
string = "hello world"
new_string = string.upper()
print(new_string) # Output: HELLO WORLD
17. strip()
Returns the string with both leading and trailing characters.
example:-
string = " this is a test "
new_string = string.strip()
print(string) # Output: " this is a test "
print(new_string) # Output: "this is a test"
18. lower()
Converts all uppercase characters in a string into lowercase.
example:-
string = "THIS IS A TEST"
new_string = string.lower()
print(string) # Output: "THIS IS A TEST"
print(new_string) # Output: "this is a test"
19. replace()
Replaces all occurrences of a substring with another substring.
example:-
string = "Hello World"
new_string = string.replace("World", "Universe")
print(new_string) # Output: "Hello Universe"
20. split()
The split()
method in Python is used to split a string into a list of substrings based on a specified separator
example:-
string = "Hello, World"
list_of_words = string.split(", ")
print(list_of_words) # Output: ["Hello", "World"]
Referances
Posted on May 10, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024