Back to Python
2026-02-175 min read

RTRIM (Python Programming)

Learn RTRIM (Python Programming) step by step with clear examples and exercises.

Title: Python RTRIM Function: A full guide

Why This Matters

In this lesson, we will delve into the Python RTRIM function, a powerful tool for removing trailing spaces from strings. Understanding and effectively using RTRIM can help you write cleaner code, prevent bugs, and impress interviewers with your problem-solving skills. Let's dive in!

Prerequisites

Before we get started, make sure you have a solid grasp of the following concepts:

  • Python basics (variables, data types, operators)
  • String manipulation in Python
  • Understanding functions and their usage
  • Familiarity with loops and control structures like for and if statements
  • Knowledge about list comprehensions

Core Concept

The RTRIM function is a built-in Python string method that removes trailing spaces from a given string. It's part of the family of trimming functions (LTRIM, RTRIM, and TRIM) that help clean up strings by removing unwanted whitespace characters.

Syntax: str.rstrip([chars])

  • str: The string you want to trim
  • [chars]: An optional argument specifying the set of characters to be removed (defaults to any whitespace character)

Here's a simple example demonstrating how RTRIM works:

my_string = " Hello, World! "
print(my_string.rstrip()) # Output: Hello, World!

In this case, the RTRIM function removed all trailing spaces from my_string. If you want to remove specific characters instead of whitespace, simply pass them as an argument within square brackets:

my_string = " ,,,Hello, World!,,,"
print(my_string.rstrip(",")) # Output: Hello, World!

Removing leading spaces

If you need to remove both leading and trailing spaces, use the LTRIM method or the TRIM function (which combines LTRIM and RTRIM):

my_string = " Hello, World! "
print(my_string.lstrip().rstrip()) # Output: Hello, World!

Or use TRIM instead:

print(my_string.strip()) # Output: Hello, World!


#### Using LTRIM and RTRIM together

While you can achieve the same result using TRIM, understanding how to use LTRIM and RTRIM separately is essential for more complex string manipulation tasks:

my_string = " Hello, World! "

print(my_string.lstrip().rstrip()) # Output: Hello, World!


### Using list comprehensions

You can use list comprehensions to apply RTRIM (or LTRIM and RTRIM) to multiple strings at once:

strings = [" john_doe ", " jane_smith ", " bob_jones ", " mary_johnson ", " jim_brown"]

clean_strings = [str.rstrip(s) for s in strings]

print(clean_strings)


Output:

['john_doe', 'jane_smith', 'bob_jones', 'mary_johnson', 'jim_brown']

Worked Example

Let's consider a real-world scenario where we need to remove trailing spaces from a list of strings containing usernames for a website registration form.

usernames = [" john_doe ", " jane_smith ", " bob_jones ", " mary_johnson ", " jim_brown"]

for username in usernames:
clean_username = username.rstrip()
print(clean_username)

Output:

john_doe
jane_smith
bob_jones
mary_johnson
jim_brown

Using a list comprehension

Alternatively, we can use a list comprehension to achieve the same result more concisely:

usernames = [" john_doe ", " jane_smith ", " bob_jones ", " mary_johnson ", " jim_brown"]
clean_usernames = [username.rstrip() for username in usernames]
print(clean_usernames)

Output:

['john_doe', 'jane_smith', 'bob_jones', 'mary_johnson', 'jim_brown']

Common Mistakes

  1. ### Forgetting to call the RTRIM method on the string

Ensure you're applying the RTRIM function to your string variable, not just assigning it to a new variable:

Incorrect:

my_string = " Hello, World! "
my_string.rstrip() # This doesn't modify my_string
print(my_string) # Output: Hello, World!

Correct:

my_string = " Hello, World! "
my_string = my_string.rstrip() # Assigns the trimmed string to my_string
print(my_string) # Output: Hello, World!
  1. ### Not specifying the characters to remove (when required)

If you need to remove specific characters other than whitespace, make sure to include them in square brackets:

Incorrect:

my_string = " ,,,Hello, World!,,,"
print(my_string.rstrip(",")) # Output: Hello, World

Correct:

my_string = " ,,,Hello, World!,,,"
print(my_string.rstrip(",,")) # Output: Hello, World!
  1. ### Assuming RTRIM removes leading spaces

RTRIM only removes trailing spaces from a given string. To remove both leading and trailing spaces, use the LTRIM method or the TRIM function (which combines LTRIM and RTRIM).

  1. ### Not handling empty strings

When using RTRIM on an empty string, it will return an empty string, as there are no trailing spaces to remove:

empty_string = ""
print(empty_string.rstrip()) # Output: ""
  1. ### Using RTRIM on lists of strings

RTRIM is a method for strings, not lists. You can loop through each item in the list and apply RTRIM to each string individually or use list comprehension as shown earlier.

  1. ### Assuming that RTRIM will remove all whitespace characters

While RTRIM removes trailing spaces, it does not remove leading spaces or other types of whitespace characters like tabs (\t) or newline characters (\n). To remove all whitespace characters, you can use the replace() method along with RTRIM:

my_string = " ,,,Hello, World!,,,\nTabbed String"
clean_string = my_string.replace(" ", "").rstrip(",\n") # Output: Hello,World!Tabbed String

Or use the translate() method to replace all whitespace characters with an empty string:

my_string = " ,,,Hello, World!,,,\nTabbed String"
clean_string = my_string.translate(str.maketrans("", "", " \t\n\r\f\v")) # Output: Hello,World!Tabbed String

Practice Questions

  1. Write a Python script that takes a list of mixed strings and numbers as input and returns a new list containing only the strings (trimmed) with trailing spaces removed.
  2. Given the following string my_string = " Hello, World! \t Tabbed String, use RTRIM to remove trailing spaces and tabs.
  3. Write a Python function that trims all whitespace characters from a given string using the RTRIM method.
  4. What will be the output of the following code snippet?
my_string = " ,,,Hello, World!,,,"
print(my_string.rstrip(","))
print(len(my_string))

FAQ

### Can I use RTRIM on lists of strings?

No, RTRIM is a method for strings, not lists. You can loop through each item in the list and apply RTRIM to each string individually or use list comprehension as shown earlier.

### Does RTRIM remove leading spaces as well?

No, RTRIM only removes trailing spaces from a given string. To remove both leading and trailing spaces, use the LTRIM method or the TRIM function (which combines LTRIM and RTRIM).

### What happens if I call RTRIM on an empty string?

Calling RTRIM on an empty string will return an empty string, as there are no trailing spaces to remove.

### How can I remove all whitespace characters from a string using RTRIM and other methods?

To remove all whitespace characters (including leading and trailing spaces), you can use the replace() method along with RTRIM:

my_string = " ,,,Hello, World!,,,"
clean_string = my_string.replace(" ", "").rstrip(",") # Output: Hello,World!

Alternatively, you can use the translate() method to replace all whitespace characters with an empty string:

my_string = " ,,,Hello, World!,,,"
clean_string = my_string.translate(str.maketrans("", "", " \t\n\r\f\v")) # Output: Hello,World!

### How can I remove all non-alphanumeric characters from a string using RTRIM and other methods?

To remove all non-alphanumeric characters (including leading and trailing spaces), you can use the translate() method along with RTRIM:

my_string = " ,,,Hello, World!,,,\nTabbed String"
clean_string = my_string.translate(str.maketrans("", "", string.ascii_letters + string.digits)) # Output: HelloWorld

### How can I remove all characters except alphanumeric and underscores from a string using RTRIM and other methods?

To remove all characters except alphanumeric and underscores (including leading and trailing spaces), you can use the translate() method along with RTRIM:

my_string = " ,,,Hello, World!,,,\nTabbed String"
clean_string = my_string.translate(str.maketrans("", "", string.ascii_letters + string.digits + "_")) # Output: HelloWorld
RTRIM (Python Programming) | Python | XQA Learn