This tutorial will explain how to remove trailing whitespace in Python using the
rstrip()
method. You will know how to strip any trailing whitespace from a string in Python.
Python has three built-in methods for trimming leading and trailing whitespace and characters from strings: strip()
, lstrip()
, and rstrip()
.
Let's get into it!
To trim a string and remove any whitespace, whether leading or trailing, use the strip()
method.
When the strip()
method has no argument, it removes both leading and trailing whitespace from a string.
So, if you have whitespace at the start or end of a word or phrase, strip()
alone, by default, will remove it.
Let's take the following example:
fave_phrase = " Hello World "
I stored the phrase " Hello World "
in a variable named fave_phrase
.
The phrase has leading whitespace — a space before the first character, H
.
The phrase also has trailing whitespace — a space after the last character, d
.
I then print the contents of fave_phrase
to the console:
print(fave_phrase)
# output
Hello World
To remove both leading and trailing whitespace from the phrase, call the strip()
method on fave_phrase
and store the result in the variable with the name trimmed_fave_phrase
like so:
fave_phrase = " Hello World "
trimmed_fave_phrase = fave_phrase.strip()
print(trimmed_fave_phrase)
# output
Hello World
Now any whitespace at the beginning and end of the string has gone!
But what if you want to remove only trailing whitespace (that is, any whitespace at the end of the string)?
Python has the rstrip()
method you can use to do just that:
fave_phrase = " Hello World "
trimmed_fave_phrase = fave_phrase.rstrip()
print(trimmed_fave_phrase)
# output
Hello World
In the example above, the leading space at the beginning of the string remains preserved, and the trailing space gets removed.
So, with rstrip()
, the whitespace gets removed only from the end of the string.
And there you have it! You now know how to strip any trailing whitespace from a string in Python.
Thanks for reading, and happy coding!
Source: https://www.freecodecamp.org
#python