Most popular String methods in Python

Last updated on February 9, 2021 The Plumber Loading... Post a comment

Python provides a lot of methods that help programmers easily and quickly modify and manipulate strings. Learning all of these methods by heart is a hard job that takes much time and effort, and you may gradually forget the ones that aren’t used frequently. So, it’s better to learn and practice popular methods and check the docs for the rarely used methods.

This article walks you through the most popular String methods in Python.

Note that string is an immutable data type and all of Python string methods return new values instead of changing the inputs.

split()

This one returns a list of strings split from the string if’s called on.

Example:

text = 'Banana, chicken, potato, fish, meat, bat, rat'
output = text.split(',')
print(output)

Output:

['Banana', ' chicken', ' potato', ' fish', ' meat', ' bat', ' rat']

Note: the split() method uses whitespace as the default separator if no parameter is passed.

join()

This method returns a string that combined from the strings in a list (most used). It can also be used to pass one string through another or reverse a string (rarely used).

Example

Combining strings in a list:

result =', '.join(['apple', 'banana', 'corn'])
print(result)

Output:

result =', '.join(['apple', 'banana', 'corn'])
print(result)

Example

Passing one string through another string:

output = '-'.join('Nothing gonna change my love for you')
print(output)

Output:

N-o-t-h-i-n-g- -g-o-n-n-a- -c-h-a-n-g-e- -m-y- -l-o-v-e- -f-o-r- -y-o-u

Example

Reversing a string:

output = ''.join(reversed('A B C D E F G'))
print(output)

Output:

G F E D C B A

replace()

This method takes a string and returns a new one with some replacement.

Example:

# replace all whitespace characters by dash characters
output = 'If tomorrow comes'.replace(' ', '-')
print(output)

Output:

If-tomorrow-comes

strip(), rstrip(), lstrip()

The strip(), rstrip(), and lstrip() methods return a new string with all passed characters (whitespace is the default) removed from the left side and right side, right side, and left side respectively.

Example:

text = '--------Banana----------'
print(text.strip('-'))
print(text.lstrip('-'))
print(text.rstrip('-'))

Output:

Banana
Banana----------
--------Banana

upper(), lower()

The upper() and lower() methods return an uppercase and lowercase string respectively.

Example:

print('a b c d e f'.upper())
print('W X Y Z'.lower())

Output:

A B C D E F
w x y z

islower(), isupper(), istitle()

The islower(), isupper(), and istitle() methods check if a string is in lowercase, uppercase, and title case respectively.

Example:

print('ANDYDOBY'.isupper())
print('rotted'.islower())
print('Dogs Have 4 Legs'.istitle())

Output:

True
True
True

isalpha(), isalnum(), isspace()

The isalpha(), isalnum(), and isspace() methods check if a string contains only alphabetic characters, alphanumeric characters, and whitespace characters respectively.

Example:

print('Abcgfjfdfjdf1230dfd'.isalnum())
print('Albert Einstein was born in 1879'.isalnum()) # Return false because of containing whitespace characters

print('abcfdfdf'.isalpha())
print(' '.isspace())

Output:

True
False
True
True

isnumeric()

Check if a string only contains numeric characters:

Example:

print('23445555'.isnumeric())
print('33ab'.isnumeric())

Output:

True
False

Conclusion

In this article, you explored several string methods that are defined in Python and are ready to use without importing any module. If you’d like to learn more about Python, check out our Python topic page for more tutorials and examples.

Related Articles

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x