AppDividend
Latest Code Tutorials

Python Trim String Example | rstrip(), lstrip(), strip() Function

1

Python provides three main methods that can be used to trim the whitespaces from the string. If you are looking for how to remove whitespaces in strings of Python, then your answer in this post. You can use one of the three Python trim string functions like strip, rstrip, and lstrip.

Python Trim String Example

Python Trim String Example | rstrip(), lstrip(), strip() Function is today’s topic. In Python programming language, the leading and trailing spaces in the string can be trimmed using the following built-in functions.

  1. strip(): It returns a new string after removing any leading and trailing whitespaces including tabs (\t).
  2. rstrip(): It returns the new string with trailing whitespace removed. It’s easier to remember as removing the white spaces from a “right” side of a string.
  3. lstrip(): It returns the new string with leading whitespace removed, or removing whitespaces from the “left” side of the string.

All of these methods don’t accept any arguments to remove whitespaces. If a character argument is provided, then they will remove those characters from the string from leading and trailing places.

If you want to count the number of characters in string then check out Python String Count article. Also, you can use the Python startswith function to check whether a string starts with a particular string.

Let’s check the strip, lstrip, and rstrip methods one by one.

#Python strip() example

Python strip() method will remove all the leading and trailing whitespace characters such as \n, \r, \t, \f, space. Python strip() method returns the copy of the string in which all chars have been stripped from the beginning and the end of the string (default whitespace characters).

See the following syntax.

string.strip([chars])

The chars parameter is optional and it the character to be removed from the beginning or end of the string.

See the following example.

# app.py

data = ' AppDividend    '
print(data)
print('After Stripped Data')
print(data.strip())

The output is the following.

Python Trim String Tutorial

You can see in the output that the Python strip() method has removed the white spaces from the beginning and end of a string.

Now, let’s pass the parameter and see the output.

# app.py

data = 'AppDividend'
print(data)
print('After Stripped d character')
print(data.strip('d'))

See the below output.

Python strip() Example

So, in the output, you can see that the last character is removed.

The original string remains as it is after using the Python strip() method.

One thing you can note down here is that it will remove the character from the start or at the end. It will not remove the character in between the string. If you try to remove the central character of the string, then it will not remove that character.

If the char argument is not given, then leading and trailing spaces will be removed.

If provided, then the strip() method will remove the matched characters from the given string.

The return value of the strip method is the copy of the string after removing the spaces or given a set of characters.

We can also pass the unique character in the parameter to remove that character. See the below code example.

# app.py

data = ' AppDividend,,------'
print(data)
print('After Stripped Data')
print(data.strip(',-'))

In the above code, we have passed the two special characters.

  1. comma(,)
  2. hyphen(-)

Now, both special characters are at the end of the string. So, it will be stripped and now see the output.

strip() Example

#Python lstrip() example

Python lstrip() method removes the whitespaces from the left side of the string and returns the new string.

See the following example.

# app.py

data = '      AppDividend'
print(data)
print('After Stripped Data')
print(data.lstrip())

See the below output.

Python lstrip()

#Python rstrip() example

Python rstrip() method removes the whitespaces from the left side of the string and returns the new string.

See the following code example.

# app.py

data = 'AppDividend       '
print(data)
print('After Stripped Data')
print(data.rstrip())

See the following example.

Python rstrip()

So, in the output, the whitespaces at the right side have been removed.

Also note that, If your source string contains leading or trailing newline (\n), return (\r) or tab (\t) characters, then those are also considered as whitespaces and will be trimmed as using the strip(), lstrip() or rstrip() methods.

#Python trim newline

We can use the rstrip() function.

Python’s rstrip() method strips all kinds of trailing whitespace by default.

# app.py

print('App Dividend\n'.rstrip())

See the output.

➜  pyt python3 app.py
App Dividend
➜  pyt

#Remove the first character of a string in Python

Let’s see the example in which we will remove the first character of the string.

# app.py

blog = 'AppDividend'
print(blog[1:])

See the output.

➜  pyt python3 app.py
ppDividend
➜  pyt

#Remove the particular character of a string in Python

For that, we need to use Python join() and Python split() function.

# app.py

blog = 'AppDividend'
print(''.join(blog.split('D', 3)))

See the output.

➜  pyt python3 app.py
Appividend
➜  pyt

Finally, Python Trim String Example | rstrip(), lstrip(), strip() Tutorial is over.

Recommended Posts

How to Check if Python String Contains Another String

Python String Contains Tutorial Example

Python String isupper islower upper-lower Functions

Python String Join Example

Python String Index Example

Python String Format Example

Python String Strip Example

Python String Find Example

Python String Replace Example

Python String Split Example

1 Comment
  1. Gus Fring says

    This is great!. I have been through this blog before and I really like what you post.
    I shared this with a friend who has a YouTube channel.
    Keep up the great work!

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.