>>> "    xyz     ".rstrip()
'    xyz'

There is more about rstrip in the documentation.

Answer from SilentGhost on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-remove-spaces-from-a-string
Remove spaces from a string in Python - GeeksforGeeks
Similarly, to remove spaces from the end of a string, we can use rstrip().
Published: July 11, 2025
Discussions

How do I remove the whitespace at the end of a string?
You need to have a different print statement for your last case. Alternatively you can rethink your solution and use something like a list join. More on reddit.com
🌐 r/learnpython
7
5
February 25, 2021
Help removing whitespace at end of output
What whitespace? I'm not seeing any. Entering the characters a, b, c and d produces the string 'd,c,b,a,' with no whitespace. Can you provide an example where the input produces undesirable output? More on reddit.com
🌐 r/learnpython
5
3
November 15, 2023
Is there a way to remove whitespace between characters in python?
import re regex = r"(/)\s+(#)" test_str = ( "// # [some comment]\n" "// # [some comment]\n" "// # [some comment]\n" "// # [some comment]" ) subst = r"\1\2" result = re.sub(regex, subst, test_str) print(result) https://regex101.com/r/sGjMQg/1 More on reddit.com
🌐 r/learnpython
12
2
July 19, 2022
Spaces, or end of line
Accidentally submitted without the body. So, I feel like my problem is very simple. I need to match a static string, which may be followed by spaces and an alphanumeric string. If there are no spaces, the static string should be an exact match. I'm using PCRE on PHP 7. Examples which should match: ABC ABC 123A ABC - 123A ABC-123A ABC- 123A ABC -123A Examples which should NOT match: ABC123A ABC More on reddit.com
🌐 r/regex
7
3
July 7, 2020
🌐
freeCodeCamp
freecodecamp.org › news › how-to-strip-trailing-whitespace-in-python
Trim a String in Python – How to Strip Trailing Whitespace
March 3, 2023 - To remove both leading and trailing ... whitespace (that is, any whitespace at the end of the string)? Python has the rstrip() method you can use to do just that:...
🌐
freeCodeCamp
freecodecamp.org › news › python-strip-how-to-trim-a-string-or-line
Python strip() – How to Trim a String or Line
January 12, 2022 - Use the .rstrip() method to remove whitespace and characters only from the end of a string. If you want to learn more about Python, check out freeCodeCamp's Python Certification. You'll start learning in an interacitve and beginner-friendly way.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-remove-spaces-from-string
Effective Ways to Remove Spaces from Strings in Python | DigitalOcean
Master Python string manipulation by learning how to remove whitespace and manage spaces with techniques like strip(), replace(), and regex for cleaner data.
🌐
DEV Community
dev.to › trinityyi › how-do-i-trim-whitespace-from-a-string-in-python-309o
How do I trim whitespace from a string in Python? - DEV Community
September 15, 2022 - Trailing whitespace characters are the whitespace characters at the end of a string. To remove them, use the str.rstrip() method. ... Do you like short, high-quality code snippets and articles?
Find elsewhere
🌐
PyTutorial
pytutorial.com › 3-ways-to-remove-whitespace-from-end-of-string
PyTutorial | 5 Ways to Remove Whitespace From End of String in Python
April 8, 2023 - import re # Define a string with whitespace at the end my_string = "Hello World " # Use a regular expression to remove whitespace from the end of the string my_string = re.sub(r'\s+$', '', my_string) # \s+$ match one or more whitespace characters ...
🌐
Codedamn
codedamn.com › news › python
Python Strip Whitespace: A Complete Guide
July 3, 2023 - The lstrip() function is used to eliminate whitespaces from the beginning of a string. Here's how it works: text = " codedamn is awesome! " new_text = text.lstrip() print(new_text) ... The rstrip() function is used to remove whitespaces from ...
🌐
Python Examples
pythonexamples.org › python-strip-remove-white-spaces-start-end-string
Trim white spaces from String in Python
In this tutorial, we will go through examples that demonstrate how to use String.strip() function to trim whitespaces from a given string. In the following example, we assign a variable with a string that has spaces at start and end of it. Then we use strip() function to remove the spaces around ...
🌐
Medium
geekpython.medium.com › ways-to-remove-whitespaces-from-the-string-in-python-f1e1481a9c33
Ways To Remove Whitespaces From The String In Python | by Sachin Pal | Medium
July 29, 2022 - It removes the leading and trailing ... method removed the leading and the trailing whitespace from the my_str. Python replace() function removes all the whitespace characters from the string including whitespace between words ...
🌐
Python Morsels
pythonmorsels.com › remove-spaces
How to remove spaces in Python - Python Morsels
January 25, 2022 - We could use the string split and join methods, as before, but join on a space character instead of an empty string: >>> version = "\tpy 310\n" >>> normalized_spaces = " ".join(version.split()) >>> normalized_spaces 'py 310' Note that this normalizes all whitespace characters (so newlines and tabs will be converted as well) and it removes spaces from the ends of our string.
🌐
DataCamp
datacamp.com › tutorial › python-trim
How to Trim a String in Python: Three Different Methods | DataCamp
February 16, 2025 - These methods include · .strip(): Removes leading and trailing characters (whitespace by default). .lstrip(): Removes leading characters (whitespace by default) from the left side of the string.
🌐
Vultr
docs.vultr.com › python › examples › trim-whitespace-from-a-string
Python Program to Trim Whitespace From a String | Vultr Docs
December 25, 2024 - Employ the rstrip() method to trim whitespace from the end (right side) of the string only. ... trailing_space_string = "End space be gone! " right_trimmed_string = trailing_space_string.rstrip() print(right_trimmed_string) Explain Code · By ...
🌐
Statistics Globe
statisticsglobe.com › home › python programming language for statistics & data science › how to remove whitespace from python string | 5 examples (strip, rstrip & lstrip)
Remove Whitespace From Python String | 5 Examples (strip, rstrip & lstrip)
March 16, 2022 - With the Python strip function, we were able to delete all left and right spaces (as shown in Example 1). However, sometimes you might want to keep the whitespace at the beginning and remove only the space at the end. For this task, we can use the rstrip Python function...
🌐
Educative
educative.io › answers › how-to-strip-whitespace-in-python
How to strip whitespace in Python
To remove whitespaces in Python, we can use the strip(), rstrip(), and lstrip() methods. We will discuss them one by one. The strip() method removes leading and trailing whitespace characters (spaces, tabs, and newline characters) from a string.
🌐
iO Flood
ioflood.com › blog › python-strip
Python strip() Function Guide (With Examples)
December 5, 2023 - The strip() method in Python is straightforward to use. It’s a built-in function that operates on string data types. Its primary purpose is to remove leading (spaces at the start) and trailing (spaces at the end) whitespace from a string.
🌐
Sentry
sentry.io › sentry answers › python › remove whitespace from a string in python
Remove whitespace from a string in Python | Sentry
July 3, 2026 - To remove all whitespace characters ... Python’s Regular Expressions module and use re.sub with r"\s", a regular expression that will match any whitespace character....
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-trim-string-rstrip-lstrip-strip
How To Trim Whitespace from a String in Python | DigitalOcean
Technical tutorials, Q&A, events — This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
🌐
CodeFatherTech
codefather.tech › home › blog › how do you remove spaces from a python string?
How Do You Remove Spaces From a Python String? - CodeFatherTech
June 27, 2025 - There are multiple ways to remove spaces from a Python string. The simplest approach is to use the string replace() method. If the spaces to remove are just at the beginning and at the end of the string the strip() method also works fine.