Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

How to remove trailing whitespace in strings in Python

In Python, you can remove trailing whitespace from a string using the rstrip() method. The rstrip() method returns a copy of the string with trailing whitespaces removed. Here's an example:

original_string = "This is a string with trailing whitespace.    "
stripped_string = original_string.rstrip()

print(f"Original string: '{original_string}'")
print(f"Stripped string: '{stripped_string}'")

In this example, rstrip() removes the trailing spaces from the original_string. If you want to remove other trailing characters, you can pass them as an argument to rstrip(). For example, to remove trailing commas and spaces, you can do:

original_string = "This is a string with trailing commas and spaces., , "
stripped_string = original_string.rstrip(" ,")

print(f"Original string: '{original_string}'")
print(f"Stripped string: '{stripped_string}'")

This will remove any trailing combination of spaces and commas from the string.

  1. Strip trailing spaces in Python string:

    • Description: Using rstrip() to remove trailing spaces from a string.
    • Code:
      original_string = "Hello World   "
      stripped_string = original_string.rstrip()
      print("Original String:", repr(original_string))
      print("Stripped String:", repr(stripped_string))
      
  2. Remove spaces at the end of string Python:

    • Description: Trimming spaces at the end of a string using rstrip().
    • Code:
      original_string = "Python is fun    "
      trimmed_string = original_string.rstrip()
      print("Original String:", repr(original_string))
      print("Trimmed String:", repr(trimmed_string))
      
  3. Python rstrip() function for whitespace:

    • Description: Using the rstrip() function to remove trailing whitespace.
    • Code:
      original_string = "Whitespace example   \t"
      stripped_string = original_string.rstrip()
      print("Original String:", repr(original_string))
      print("Stripped String:", repr(stripped_string))
      
  4. Trim trailing whitespace Python:

    • Description: Trimming trailing whitespace using rstrip().
    • Code:
      original_string = "   Trim trailing spaces   "
      trimmed_string = original_string.rstrip()
      print("Original String:", repr(original_string))
      print("Trimmed String:", repr(trimmed_string))
      
  5. Strip trailing newline in Python string:

    • Description: Using rstrip() to strip trailing newlines from a string.
    • Code:
      original_string = "Python is great\n\n"
      stripped_string = original_string.rstrip('\n')
      print("Original String:", repr(original_string))
      print("Stripped String:", repr(stripped_string))
      
  6. Clean whitespace from the end of string in Python:

    • Description: Cleaning whitespace from the end of a string using rstrip().
    • Code:
      original_string = "   Clean whitespace   \n\t"
      cleaned_string = original_string.rstrip()
      print("Original String:", repr(original_string))
      print("Cleaned String:", repr(cleaned_string))
      
  7. Remove trailing tabs from string in Python:

    • Description: Removing trailing tabs from a string using rstrip('\t').
    • Code:
      original_string = "Python with tabs\t\t\t"
      stripped_string = original_string.rstrip('\t')
      print("Original String:", repr(original_string))
      print("Stripped String:", repr(stripped_string))
      
  8. Python regex to remove trailing spaces:

    • Description: Using regular expressions to remove trailing spaces.
    • Code:
      import re
      
      original_string = "   String with spaces   "
      trimmed_string = re.sub(r'\s+$', '', original_string)
      print("Original String:", repr(original_string))
      print("Trimmed String:", repr(trimmed_string))
      
  9. Trim whitespace at the end of each line in Python:

    • Description: Trimming whitespace at the end of each line using a loop.
    • Code:
      multiline_string = "Line 1   \nLine 2   \nLine 3   \n"
      trimmed_lines = [line.rstrip() for line in multiline_string.split('\n')]
      cleaned_string = '\n'.join(trimmed_lines)
      print("Original String:")
      print(multiline_string)
      print("Cleaned String:")
      print(cleaned_string)