10 Useful String Methods in Python

Introduction

Generally, a string is consists of one or more characters. These characters can be the alphabet, numbers, etc.

In most cases, we need to manipulate string as per our need. It is tedious to do this manually. That’s why general-purpose programming languages come with so many useful methods to make our life easier. Similarly, Python has several methods that make our life easier. This article is all about string methods that I find useful.

So we are going to see the 10 most useful methods with the code example. These 10 methods lists are randomly ordered.

Remember: These methods returns a completely new string. The original string will remain the same.

upper()

Pretty simple method and self-explanatory. This method converts a string into uppercase. Yes, you are right. A string might contain numbers, but It only applies to the alphabetic characters. Let’s see the following code example.

string = 'Python'
string.upper()
print(string) # Python

new_string = string.upper()
print(new_string) # PYTHON

print('Python123'.upper()) # 'PYTHON123'

lower()

This method is the exact opposite of the previous one. This method converts your string into lowercase. See the example.

string = 'PYTHON'
new_string = string.lower()
print(new_string) # 'python'

capitalize()

This method only changes the first letter from a string. It capitalizes the first letter only. There is another method called title(). It’s capitalized all word’s first letter from a string.
Let’s see a practical example of both methods.

# capitalize()

string = 'python is awesome!'
new_string = string.capitalize()
print(new_string) # Python is awesome!

# title()
string = 'python is awesome!'
new_string = string.title()
print(new_string) # Python Is Awesome!

count()

This method takes a parameter. It returns how many times the given parameter occurs in a string. Pretty straightforward. See the following example.

# Example 1
string = 'Python is awesome'
result = string.count('o')
print(result) # 2

# Example 2
string = 'Python Python is a programming language.'
result = string.count('Python')
print(result) # 2

find()

By using this method, we can get the position of the given value in a string. As usual, this method takes a parameter. If the given value is not in the string, it will returns -1.

Let’s an example.

string = 'Python'

result1 = string.find('P')
result2 = string.find('n')
result3 = string.find('a')

print(result1) # 0
print(result2) # 5
print(result3) # -1

index()

This one is similar to the previous method (find). It also returns the index of the given value. But the difference is, if the given value is not in the string, it will give us ValueError instead of returning -1.
Let’s see the example.

string = 'Python'
result = string.index('a')

print(result)

# ...
# ValueError: substring not found

isalnum()

This method returns a boolean based on the string. If a string contains both alphabet and numbers or any of them, then it will return True. Otherwise, False.

Similarly, another method called isalpha() that returns True if a string contains only the alphabet.

Let’s see the following example for a better understanding.

# isalnum()

# Example 1
string = 'abc123'
result = string.isalnum()
print(result) # True

# Example 2
string = 'abc_123!'
result = string.isalnum()
print(result) # False

# isalpha()

# Example 1
string = 'abc'
result = string.isalpha()
print(result) # True

# Example 2
string = 'abc123'
result = string.isalpha()
print(result) # False

isdigit()

This method checks each of the characters in a string is a digit or not. If all characters are digit, then this method returns True. Otherwise, False. Let’s see an example.

# Example 1

string = '123'
result = string.isdigit()
print(result) # True

# Example 2

string = 'hello123'
result = string.isdigit()
print(result) # False

strip()

This one is used to trim a string. Generally, this method trims from both sides and trim only space.

If we provide a parameter to the method, then it will trim based on the parameter value. Let’s see how it works.

# Example 1

string = '  Python '
result = string.strip()
print(result) # Python

# Example 2

string = '###Python###'
result = string.strip('#')
print(string) # ###Python###
print(result) # Python

lstrip() and rstrip() two methods also trim a string. But lstrip() trims a string’s only left side. And rstrip() trims only right side.

string = '###Python###'
print(string.lstrip('#')) # Python###
print(string.rstrip('#')) # ###Python

replace()

This method is used for replacing something from a string with a new one. This method takes two parameters. The first one is the value we want to remove, and the second is the value we want to put in place of the first value.

Let’s see an example to understand better.

string = 'Laravel is a Python framework.'
new_string = string.replace('Laravel', 'Django')

print(string) # Laravel is a Python framework.
print(new_string) # Django is a Python framework.

Note: Laravel is a PHP framework.

Conclusion

There are so many string methods. But these methods are really effective. I use these methods a lot. I hope this article will be useful to you.

Scroll to Top