AppDividend
Latest Code Tutorials

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

0

Python Trim String Tutorial | rstrip(), lstrip(), strip() Example is today’s topic. In Python programming language, the leading and trailing spaces can be trimmed by using the built-in functions. Python provides three mainly methods that can be used to trim the whitespaces from the string. If you have a question like How to remove whitespaces in strings of Python, then your answer is this post. You can use one of the three functions like strip, rstrip, and lstrip functions. 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.

Python Trim String Tutorial

  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 a 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.

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

Python strip()

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 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 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 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 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()

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()

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.

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

Leave A Reply

Your email address will not be published.

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