Strings are the most common basic data type in Python. Because of that, there are several things you can do with them via methods. This post highlights 12 basic string methods you should get familiar with as they can help you modify your code in several ways.
You may use some of them often while others you may just want to learn for fun.
New to Python? Discover different ways of writing strings not just with a double quote:
Jump to Section 👇
12 String Methods to Improve Your Code
upper( )
As the name implies, this method takes your string and converts all the characters to uppercase.
text = 'Welcome to DataScience Ville!'
print(text.upper())
# OUTPUT: WELCOME TO DATASCIENCE VILLE!
lower( )
In contrast with the upper
method, this method takes your string and converts all the characters to lowercase.
text = 'Welcome to DataScience Ville!'
print(text.lower())
# OUTPUT: welcome to datascience ville!
title( )
The same rule applies here. It converts your string to the title case.
text = 'Welcome to DataScience Ville!'
print(text.title())
# OUTPUT: Welcome To Datascience Ville!
capitalize( )
It may seem confusing but this isn’t the same as the upper
method. The capitalize
method only converts the first character into the uppercase.
text = 'Welcome to DataScience Ville!'
print(text.capitalize())
# OUTPUT: Welcome to datascience ville!
join( )
A really useful method used to convert a list or tuple into a single string, separated by a delimiter you specify.
text = ['Welcome', 'to', 'DataScience Ville!']
print(' '.join(text)) # Here, space (' ') is the delimiter
# OUTPUT: Welcome to DataScience Ville!
split( )
This method takes a string and splits it at a specified value and then returns a list. Here’s an extreme example:
text = 'Welcome to DataScience Ville!'
print(text.split('e'))
# OUTPUT: ['W', 'lcom', ' to DataSci', 'nc', ' Vill', '!']
# Here, e is the point at which the string is seperated.
DID YOU KNOW❔
You can also combine string methods.
For instance, you can combine the join( ) and split( ) methods to get rid of any unnecessary spacing in your string.
text = ' Welcome to DataScience Ville! '
cleaned_text = ' '.join(text.split())
print(cleaned_text)
# OUTPUT: Welcome to DataScience Ville!
In the above example, the
split()
method splits thetext
string into['Welcome', 'to', 'DataScience', 'Ville!']
by removing all whitespace.Afterward, the
join()
method then joins these substrings with a single space between each one, resulting in'Welcome to DataScience Ville!'
.
replace( )
It allows you to replace a value within a string with another value.
text = 'Welcome to DataScience Ville!'
print(text.replace('Welcome to', 'This is'))
# OUTPUT: This is DataScience Ville!
isalpha( )
isalpha
checks if all characters in a string are alphabetic and returns a boolean value(True or False). If even a number or a space exists, it returns false
.
text = 'Welcome to DataScience Ville!'
print(text.isalpha())
# OUTPUT: False
isdigit( )
It checks if all characters in a string are numeric digits. It will also return false
if a space exists.
text = '100'
print(text.isdigit())
# OUTPUT: True
isalnum( )
It checks if all characters in a string are alphabetic and numeric. It’s like a combination of both isalpha
and isdigit
methods.
text = 'DSV100'
print(text.isalnum())
# OUTPUT: True
count( )
It counts the number of times a value appears within a string.
text = 'Welcome to DataScience Ville!'
print(text.count('e'))
# OUTPUT: 5
find( )
It searches a string for a value and returns the index position of the value.
text = 'Welcome to DataScience Ville!'
print(text.find('e'))
# OUTPUT: 1
# NOTE: Index of spaces are also counted
Each string method has a specific use case. And as you keep coding, you’ll discover the best scenarios you wish to use them.
FAQs Concerning Strings
What is a string in Python?
A string is basically anything enclosed within quotes in Python. It can be words, numbers, other quotes, or even spaces.
What are Python string methods?
Python string methods are built-in functions of Python that you can use to modify a string.
How many Python string methods are there?
There are 47 string methods in Python. You can find a list of all of them in Python’s official documentation of strings.
How to count the length of a string in Python?
You can count the length of a string by making use of the len( )
function. Within the brackets, place your string and you’ll get the total number of characters in return. Here’s an example:
text = 'Welcome to DataScience Ville!'
length = len(text)
print(length) # Output: 28
How can a string be broken in Python?
As seen above, a string can be broken in Python by using the split( ) method.
Can we convert a list to a string in Python?
Yes. You can do this by using the join( ) method.
Can you add two strings in Python?
Yes, you can add multiple strings in Python using a concatenation operator (+
). Here’s a basic example:
text1 = 'Welcome to '
text2 = 'DataScience Ville!'
result = text1 + text2
print(result) # Output: Welcome to DataScience Ville!
Can Python multiply strings?
Yes, you can multiply strings using the *
operator. It’ll repeat the string a specified number of times.
text = 'DataScience Ville! '
result = string * 3
print(result) # Output: DataScience Ville! DataScience Ville! DataScience Ville!
Why do we need Python strings?
Strings are essential in Python because they allow us to work with text data. Since they can also store letters, numbers, and special characters, they are the most versatile data type in Python.
Can you modify a string after it is created?
No, strings in Python are immutable, which means once a string is created, it cannot be changed. Instead, any modification creates a new string.