Remove end space in python

Asked 12 years, 6 months ago

Viewed 205k times

I need to remove whitespaces after the word in the string. Can this be done in one line of code?

Example:

string = "    xyz     "

desired result : "    xyz" 

asked Mar 3, 2010 at 15:36

4

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

There is more about rstrip in the documentation.

Remove end space in python

answered Mar 3, 2010 at 15:37

SilentGhostSilentGhost

293k64 gold badges301 silver badges291 bronze badges

You can use strip() or split() to control the spaces values as the following, and here is some test functions:

words = "   test     words    "

# Remove end spaces
def remove_end_spaces(string):
    return "".join(string.rstrip())

# Remove first and  end spaces
def remove_first_end_spaces(string):
    return "".join(string.rstrip().lstrip())

# Remove all spaces
def remove_all_spaces(string):
    return "".join(string.split())

# Remove all extra spaces
def remove_all_extra_spaces(string):
    return " ".join(string.split())

# Show results
print(f'"{words}"')
print(f'"{remove_end_spaces(words)}"')
print(f'"{remove_first_end_spaces(words)}"')
print(f'"{remove_all_spaces(words)}"')
print(f'"{remove_all_extra_spaces(words)}"')

output:

"   test     words    "

"   test     words"

"test     words"

"testwords"

"test words"

i hope this helpful .

answered Nov 3, 2020 at 14:48

K.AK.A

8808 silver badges18 bronze badges

2

There are various ways to remove spaces from a string in Python. This tutorial is aimed to provide a short example of various functions we can use to remove whitespaces from a string.

Python Remove Spaces from String

Python String is immutable, so we can’t change its value. Any function that manipulates string value returns a new string, and we have to explicitly assign it to the string. Otherwise, the string value won’t change. Let’s say we have an example string defined as:

s = '  Hello  World   From Pankaj \t\n\r\tHi There  '

This string has different types of whitespaces as well as newline characters. Let’s have a look at different functions to remove spaces.

strip()

Python String strip() function will remove leading and trailing whitespaces.

>>> s.strip()
'Hello  World   From Pankaj \t\n\r\tHi There'

If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.

replace()

We can use replace() to remove all the whitespaces from the string. This function will remove whitespaces between words too.

>>> s.replace(" ", "")
'HelloWorldFromPankaj\t\n\r\tHiThere'

join() with split()

If you want to get rid of all the duplicate whitespaces and newline characters, then you can use join() function with string split() function.

>>> " ".join(s.split())
'Hello World From Pankaj Hi There'

translate()

If you want to get rid of all the whitespaces as well as newline characters, you can use string translate() function.

>>> import string
>>> s.translate({ord(c): None for c in string.whitespace})
'HelloWorldFromPankajHiThere'

Remove end space in python

Python Remove Whitespaces from String using Regex

We can also use a regular expression to match whitespace and remove them using re.sub() function.

import re

s = '  Hello  World   From Pankaj \t\n\r\tHi There  '

print('Remove all spaces using RegEx:\n', re.sub(r"\s+", "", s), sep='')  # \s matches all white spaces
print('Remove leading spaces using RegEx:\n', re.sub(r"^\s+", "", s), sep='')  # ^ matches start
print('Remove trailing spaces using RegEx:\n', re.sub(r"\s+$", "", s), sep='')  # $ matches end
print('Remove leading and trailing spaces using RegEx:\n', re.sub(r"^\s+|\s+$", "", s), sep='')  # | for OR condition

Output:

Remove all spaces using RegEx:
HelloWorldFromPankajHiThere
Remove leading spaces using RegEx:
Hello  World   From Pankaj 	
	Hi There  
Remove trailing spaces using RegEx:
  Hello  World   From Pankaj 	
	Hi There
Remove leading and trailing spaces using RegEx:
Hello  World   From Pankaj 	
	Hi There

Remove end space in python

You can check out the complete Python script and more Python examples from our GitHub Repository.

Reference: StackOverflow Question

How do I remove spaces at the end of Python?

strip() Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.

How do I remove spaces at the beginning and end of a string in Python?

Use the . strip() method to remove whitespace and characters from the beginning and the end of a string. Use the . lstrip() method to remove whitespace and characters only from the beginning of a string.

How do you remove a space at the end of a string?

String result = str. The trim() method will remove both leading and trailing whitespace from a string and return the result.

How do I remove print spaces in Python?

By using strip() method. By using replace() method..
strip(): This function removes the leading and the trailing spaces from an array including the tabs(\t)..
lstrip(): This function removes spaces from the left end of the string..
rstrip(): This function removes spaces from the right end of the string..