How To Remove Trailing Slash In Python

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

Use .rstrip() Method

The .rstrip() 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 ends with those set of characters. If there is no parameter set the method removes all trailing whitespace characters.

Here is a demonstration of the .rstrip() method at work:

>>> url = "/"
>>> url.rstrip('/')
'"
>>> print(url)
'/

As you can see from the above example, using the .rstrip() method does not mutate (change) the original string instead it returns a new string.

The benefit of using the .rstrip() method is that it strips multiple characters should there be the same character at the end of the string.

Here is a demonstration showing the benefit of using the .rstrip() 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 .rstrip() 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 my_string[:-1] 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 = "/"
>>> url[:-1] if url[-1] == '/' else url
''

As you can see from the above code the slice operator url[:-1] 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.

Summary

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 es from a string use the built-in .rstrip() string method. To remove just a single trailing slash from a string use the slice operator my_string[:-1] . Both approaches return a new string and do not mutate the original string.

Photo of author
Ryan Sheehy
Ryan has been dabbling in code since the late '90s when he cut his teeth exploring VBA in Excel. Having his eyes opened with the potential of automating repetitive tasks, he expanded to Python and then moved over to scripting languages such as HTML, CSS, Javascript and PHP.