AppDividend
Latest Code Tutorials

Python trim string: How to trim Whitespace in Python

To count the number of characters in a string, check out the string count post. You can also use the Python startswith function to check whether a string starts with a particular string. First, let’s check the strip, lstrip, and rstrip methods.

Python trim

To trim a string in Python, use the strip() function. The strip() is a built-in function that trims the string and removes leading and trailing white spaces. To remove whitespace from left-side, use the lstrip() function. To remove whitespace from the right side, use the rstrip() function.

  1. strip(): The strip() returns a new string after removing any leading and trailing whitespaces, including tabs (\t).
  2. rstrip(): The rstrip() returns the new string with trailing whitespace removed. It’s more accessible to remember by removing the white spaces from a “right” side of a string.
  3. lstrip(): The lstrip() returns the new string with the 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. However, if a character argument is provided, they will remove those characters from the string from leading and trailing places.

Python strip

The strip() is a built-in Python method that removes all the leading and trailing whitespace characters such as \n, \r, \t, \f, space. In addition, the 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).

Syntax

See the following syntax.

string.strip([chars])

Arguments

The chars parameter is optional, and it is 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 after using the Python strip() method. You can note that it will remove the character from the start or at the end. It will not remove the character in between the string. So 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 giving 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. the 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()

The lstrip() is a built-in Python method that removes the whitespaces from the left side of the string and returns the new string. The lstrip() function does not take any arguments.

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

The rstrip() is a built-in Python method that removes the whitespaces from the left side of the string and returns the new string. The strip() method does not take any arguments.

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 on 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

To trim the whitespace in Python, 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

Removing the first character of a string in Python.

To remove the first character of a string in Python, use the string slicing using the colon(:) operator.

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

# app.py

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

See the output.

➜  pyt python3 app.py
ppDividend
➜  pyt

Removing the particular character

To remove the specific character from a string in Python, use the combination of join() and split() function.

# app.py

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

See the output.

➜  pyt python3 app.py
Appividend
➜  pyt

That’s it for this tutorial.

Recommended Posts

How to check if Python String contains another String

Python string contains()

Python string isupper islower upper-lower Functions

Python string join()

Python string index()

Python string format()

Python string find()

Python string replace()

Python string split()

2 Comments
  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!

  2. Jim says

    Using python 3.8 on Windows 7, the strip method will not remove the linefeed character, even if I explicitly specify the set of chars to strip as ‘ \r\n\t’

Leave A Reply

Your email address will not be published.

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