5 most useful string modifying methods in Python (18/100 Days of Python)

Martin Mirakyan
2 min readJan 19, 2023

--

Day 18 of the “100 Days of Python” blog post series covering string modifying methods

There are different methods available in Python that help to modify strings. In this tutorial, we’ll cover 5 of the most popular ones, and look at some examples of how to use them:

  1. str.replace(old, new): This method replaces all occurrences of a specified old substring in a string with a new one:
greeting = 'Hello World'
print(greeting.replace('World', 'Universe')) # Hello Universe
print(greeting.replace('Hey', 'Universe')) # Hello World
print('abaabab'.replace('ab', 'AB')) # ABaABAB

2. str.strip() method removes any whitespace characters at the beginning or end of the string, including spaces, tabs, and newlines. By default the strip() method removes all the whitespaces. Yet, it can be used to remove any specified sequence of cahracters:

greeting = '      Hello World!   '
print(greeting.strip()) # `Hello World!`
print('Hey Universe'.strip()) # `Hey Universe`
print(' abaabab'.strip()) # `abaabab`
print('__hello__'.strip('_')) # `hello`
print(' _hey hey___'.strip('_')) # ` _hey hey`

3. str.lstrip() can be used similarly to the strip() method to remove whitespace characters from the left (beginning) of the given string. It can also accept a parameter of which characters should be removed in case you don’t want to remove whitespaces:

greeting = '      Hello World!   '
print(greeting.lstrip()) # `Hello World! `
print('Hey Universe'.lstrip()) # `Hey Universe`
print(' abaabab'.lstrip()) # `abaabab`
print('__hello__'.lstrip('_')) # `hello__`
print(' _hey hey___'.lstrip('_')) # ` _hey hey___`

4. str.rstrip() performs the same operation as the lstrip but from the right part of the string:

greeting = '      Hello World!   '
print(greeting.rstrip()) # ` Hello World!`
print('Hey Universe'.rstrip()) # `Hey Universe`
print(' abaabab'.rstrip()) # ` abaabab`
print('__hello__'.rstrip('_')) # `__hello`
print(' _hey hey___'.rstrip('_')) # ` _hey hey`

5. str.removeprefix() can remove a certain sequence of characters from the beginning of the string once. In case of the lstrip() the program removes as many occurances of the specified character (or the whitespace) as it finds, while in case of the removeprefix() this operation is performed only once:

greeting = '      Hello World!   '
print(greeting.removeprefix(' ')) # ` Hello World! `
print('Hey Universe'.removeprefix('H'))) # `ey Universe`
print(' abaabab'.removeprefix(' a')) # `baabab`
print('__hello__'.removeprefix('_')) # `_hello__`
print(' _hey hey___'.rstrip(' ')) # ` _hey hey___`

6. Bonus: str.removesuffix() does the same thing as removeprefix() but removes the sequence of characters from the very end of the string.

These were some of the useful methods to modify strings in Python. There are many more that are available, but we covered some of the most helpful ones here that are used often.

What’s next?

--

--

Martin Mirakyan
Martin Mirakyan

Written by Martin Mirakyan

Software Engineer | Machine Learning | Founder of Profound Academy (https://profound.academy)

Responses (1)