How do you remove a trailing slash in a string using Python?
There are two popular ways to remove a trailing slash from a string in Python. The most common approach is to use the
string method which will remove
all consecutive trailing slashes
at the end of a string. If you only need to
remove one trailing slash
the string slice operator can be another alternative.
Here’s how each approach works.
string method takes a set of characters as its sole parameter and returns a new string by removing the characters in the original string if the string
with those set of characters. If there is no parameter set the method removes all trailing whitespace characters.
Here is a demonstration of the
method at work:
>>> url = "https://scripteverything.com/" >>> url.rstrip('/') 'https://scripteverything.com" >>> print(url) 'https://scripteverything.com/
As you can see from the above example, using the
method does not mutate (change) the original string instead it returns a new string.
The benefit of using the
method is that it strips
characters should there be the same character at the end of the string.
Here is a demonstration showing the benefit of using the
method when removing the same character multiple times from the end of a string:
>>> url = "https://" >>> url.rstrip('/') 'http:'
As you can see from the above output using the
method strips away all occurrences of the character
if it is found at the end of the string together.
If though you only want to remove one instance of the character then you might want to try another alternative.
Use Slice Operator To Remove Trailing Slash
Another way to remove the trailing slash character from a string is to use the slice operator. If you know a string ends with the trailing slash character then you can use
and this will remove the last character from the string.
You may want to use the if-else one-line statement to check the string ends with a trailing slash and you can do this using the slice operator too.
Here’s an example demonstrating this approach:
>>> url = "https://scripteverything.com/" >>> url[:-1] if url[-1] == '/' else url 'https://scripteverything.com'
As you can see from the above code the slice operator
fetches all characters from the original string starting from the first character and going to the last character (excluding the last character). The other slice operator in the
one line if else ternary operation
checks the last character of the string to see if it matches the trailing slash.
This method will only check the last character and will not remove multiple trailing characters from the original string.
Removing a trailing slash from a string is useful when dealing with URLs and directories that are strings, especially when there will be appending of paths to the original root directory.
To remove consecutive trailing slash
from a string use the built-in
string method. To remove just a single trailing slash from a string use the slice operator
. Both approaches return a new string and do not mutate the original string.