DEV Community

Ankit Kumar
Ankit Kumar

Posted on

String Methods in Python

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
Enter fullscreen mode Exit fullscreen mode

2. center()

Pad the string with the specified character.
example:-

my_string= "ankit"
print(my_string.center(11,'_')) : ___ankit___
Enter fullscreen mode Exit fullscreen mode

3. count()

Returns the number of occurrences of a substring in the string.
example:-

 my_string= "ankitki"
print(my_string.count("ki")) : 2
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

6. find()

Returns the lowest index of the substring if it is found.
example:-

my_string= "ankitkumar"
print(my_string.find("ku")) : 5
Enter fullscreen mode Exit fullscreen mode

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."
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

13. isnumeric()

Returns “True” if all characters in the string are numeric characters.
example:-

string1 = "12345"
print(string1.isnumeric())  # Output: True
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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"]
Enter fullscreen mode Exit fullscreen mode

Referances

Python docs
Geeks for Geeks

Top comments (0)