How do I trim whitespace from a string?
Asked Answered
R

14

1365

How do I remove leading and trailing whitespace from a string in Python?

" Hello world " --> "Hello world"
" Hello world"  --> "Hello world"
"Hello world "  --> "Hello world"
"Hello world"   --> "Hello world"
Rearm answered 17/4, 2009 at 19:16 Comment(3)
Just to let more people know about rstrip pitfalls. 'WKHS.US.TXT'.rstrip('.US.TXT') will return WKH rather than WKHS. This rstrip creates a BUG that is difficult to troubleshoot for me.Prairial
Agree. the argument to rstrip is a list of characters that should stripped off from the end of string. Hence, 'WKHS' has the suffix 'S' which is also a char we asked the rstrip to remove. After this, 'H' comes. It will be character that is not part of argument. Stripping stops as soon as it can't strip character under question.Bywaters
Just do .split('.')[0] then tbhBufflehead
R
1990

To remove all whitespace surrounding a string, use .strip(). Examples:

>>> ' Hello '.strip()
'Hello'
>>> ' Hello'.strip()
'Hello'
>>> 'Bob has a cat'.strip()
'Bob has a cat'
>>> '   Hello   '.strip()  # ALL consecutive spaces at both ends removed
'Hello'

Note that str.strip() removes all whitespace characters, including tabs and newlines. To remove only spaces, specify the specific character to remove as an argument to strip:

>>> "  Hello\n  ".strip(" ")
'Hello\n'

To remove only one space at most:

def strip_one_space(s):
    if s.endswith(" "): s = s[:-1]
    if s.startswith(" "): s = s[1:]
    return s

>>> strip_one_space("   Hello ")
'  Hello'
Reckon answered 17/4, 2009 at 19:21 Comment(16)
If you need the strip function, for example a map function, you can access it via str.strip(), like so map(str.strip, collection_of_s)Extremism
Is there a way to only trim the whitespaces at the ends?Belle
@NikhilGirraj strip() trims whitespace at the ends by default: docs.python.org/2/library/string.htmlMoan
@Moan Thanks for the reference, but I think you meant the rstrip() function. :-)Belle
@NikhilGirraj ah, so I could have worded that better. What I meant by "ends" was "places where word characters start and stop. Like a physical piece of string. You meant the rightmost end. no worries =)Moan
Sometimes I feel like python purposely avoids the well-accepted and meaningful names that the vast majority of languages use in order to be "unique" and "different" - strip instead of trim, isinstance instead of instanceof, list instead of array, etc, etc. Why not just use the names everyone is familiar with?? geez :PVermouth
@GershomMaes in strip's case, I completely agree, but a list is completely different from an array.Anew
You can specify which side is trimmed using lstrip() or rstrip()Awful
@JFA, that was precisely @GershomMaes’ point. What Python calls a “list” is what anyone sane calls an “array”, a very regrettable confusion…Biyearly
Plus one for the "why confuse standard naming" cry. I definitely dislike when someone just names things differently for no other purpose but to make it different. For me, "re.sub()" was one of the biggest punch: if Python aims at concise and readable code, well, this is the exact opposite. Now this trim-strip thing is my new favourite. I mean, why call it TV when you can call it Fernseh, right?Bastardy
@Maëlan I'm not sure who "anyone sane" is, but C++, Python, Java and C# call an ordered, dynamically resizable collection of elements a list. Ruby and Javascript call it an array. Rust calls it a vector (I think). ¯_(ツ)_/¯Spinifex
@RobertGrant, I apologize if my previous message was rude. To me, your description of Python “lists” omits an essential feature: (amortized-constant-time push/pop and) constant-time random access. Because Python “lists” actually are dynamic arrays (contiguous storage). By contrast, linked lists only have linear-time random access, in exchange for constant-time push/pop. I believe that people more commonly understand “lists” as linked lists (and the like). But I may be biased myself.Biyearly
So I conducted a quick terminological survey to get to the bottom of it. In C++/Rust/Java, an “array” is a contiguous storage, and a “vector” is a dynamic array. In Ruby/JS, an “array” is a dynamic array. Additionally in C++ and JS, a “list” is a (doubly) linked list. In the functional programming world (Haskell, ML and friends), a “list” is always a (persistent) linked list. However in Java, a “list” is any ordered collection, be it linked lists or (dynamic) arrays. Wikipedia says the same. This is a weaker definition than yours.Biyearly
I'm not offended :-) I just disagree that list is an insane name. On Java, it calls a Python list an ArrayList; i.e. a list interface backed by an array implementation. Java's vector is a list + thread safety.Spinifex
@Maëlan I'm confused. Doesn't an array have to have a fixed type or fixed element-size in order to work properly? The closest I could imagine would be a dynamic array of pointers that point towards the actual items.Donaugh
I love how comment section for strip() function in python turned into a discussion about Java's collections terminology XDWearisome
C
284

As pointed out in answers above

my_string.strip()

will remove all the leading and trailing whitespace characters such as \n, \r, \t, \f, space .

For more flexibility use the following

  • Removes only leading whitespace chars: my_string.lstrip()
  • Removes only trailing whitespace chars: my_string.rstrip()
  • Removes specific whitespace chars: my_string.strip('\n') or my_string.lstrip('\n\r') or my_string.rstrip('\n\t') and so on.

More details are available in the docs.

Capet answered 18/5, 2011 at 4:16 Comment(2)
i believe is \r\n not \n\r ... (can't edit the post - not enough chars modified)Unawares
@StefanNch: The order of the characters does not matter at all. \n\r will also remove \r\n.Habsburg
W
136

strip is not limited to whitespace characters either:

# remove all leading/trailing commas, periods and hyphens
title = title.strip(',.-')
Worsted answered 17/4, 2012 at 13:22 Comment(0)
H
63

This will remove all leading and trailing whitespace in myString:

myString.strip()
Humanitarian answered 17/4, 2009 at 19:19 Comment(0)
W
31

You want strip():

myphrases = [" Hello ", " Hello", "Hello ", "Bob has a cat"]

for phrase in myphrases:
    print(phrase.strip())
Weisman answered 17/4, 2009 at 19:21 Comment(1)
print( [ phrase.strip() for phrase in myphrases ] )Papiamento
U
3

This can also be done with a regular expression

import re

input  = " Hello "
output = re.sub(r'^\s+|\s+$', '', input)
# output = 'Hello'
Uranyl answered 2/5, 2020 at 0:18 Comment(0)
B
1

Well seeing this thread as a beginner got my head spinning. Hence came up with a simple shortcut.

Though str.strip() works to remove leading & trailing spaces it does nothing for spaces between characters.

words=input("Enter the word to test")
# If I have a user enter discontinous threads it becomes a problem
# input = "   he llo, ho w are y ou  "
n=words.strip()
print(n)
# output "he llo, ho w are y ou" - only leading & trailing spaces are removed 

Instead use str.replace() to make more sense plus less error & more to the point. The following code can generalize the use of str.replace()

def whitespace(words):
    r=words.replace(' ','') # removes all whitespace
    n=r.replace(',','|') # other uses of replace
    return n
def run():
    words=input("Enter the word to test") # take user input
    m=whitespace(words) #encase the def in run() to imporve usability on various functions
    o=m.count('f') # for testing
    return m,o
print(run())
output- ('hello|howareyou', 0)

Can be helpful while inheriting the same in diff. functions.

Burkley answered 17/6, 2020 at 17:58 Comment(0)
O
1

In order to remove "Whitespace" which causes plenty of indentation errors when running your finished code or programs in Pyhton. Just do the following;obviously if Python keeps telling that the error(s) is indentation in line 1,2,3,4,5, etc..., just fix that line back and forth.

However, if you still get problems about the program that are related to typing mistakes, operators, etc, make sure you read why error Python is yelling at you:

The first thing to check is that you have your indentation right. If you do, then check to see if you have mixed tabs with spaces in your code.

Remember: the code will look fine (to you), but the interpreter refuses to run it. If you suspect this, a quick fix is to bring your code into an IDLE edit window, then choose Edit..."Select All from the menu system, before choosing Format..."Untabify Region. If you’ve mixed tabs with spaces, this will convert all your tabs to spaces in one go (and fix any indentation issues).

Oahu answered 4/8, 2020 at 16:35 Comment(0)
S
0

I could not find a solution to what I was looking for so I created some custom functions. You can try them out.

def cleansed(s: str):
    """:param s: String to be cleansed"""
    assert s is not (None or "")
    # return trimmed(s.replace('"', '').replace("'", ""))
    return trimmed(s)


def trimmed(s: str):
    """:param s: String to be cleansed"""
    assert s is not (None or "")
    ss = trim_start_and_end(s).replace('  ', ' ')
    while '  ' in ss:
        ss = ss.replace('  ', ' ')
    return ss


def trim_start_and_end(s: str):
    """:param s: String to be cleansed"""
    assert s is not (None or "")
    return trim_start(trim_end(s))


def trim_start(s: str):
    """:param s: String to be cleansed"""
    assert s is not (None or "")
    chars = []
    for c in s:
        if c is not ' ' or len(chars) > 0:
            chars.append(c)
    return "".join(chars).lower()


def trim_end(s: str):
    """:param s: String to be cleansed"""
    assert s is not (None or "")
    chars = []
    for c in reversed(s):
        if c is not ' ' or len(chars) > 0:
            chars.append(c)
    return "".join(reversed(chars)).lower()


s1 = '  b Beer '
s2 = 'Beer  b    '
s3 = '      Beer  b    '
s4 = '  bread butter    Beer  b    '

cdd = trim_start(s1)
cddd = trim_end(s2)
clean1 = cleansed(s3)
clean2 = cleansed(s4)

print("\nStr: {0} Len: {1} Cleansed: {2} Len: {3}".format(s1, len(s1), cdd, len(cdd)))
print("\nStr: {0} Len: {1} Cleansed: {2} Len: {3}".format(s2, len(s2), cddd, len(cddd)))
print("\nStr: {0} Len: {1} Cleansed: {2} Len: {3}".format(s3, len(s3), clean1, len(clean1)))
print("\nStr: {0} Len: {1} Cleansed: {2} Len: {3}".format(s4, len(s4), clean2, len(clean2)))
Starlin answered 11/4, 2019 at 7:43 Comment(0)
C
0

If you want to trim specified number of spaces from left and right, you could do this:

def remove_outer_spaces(text, num_of_leading, num_of_trailing):
    text = list(text)
    for i in range(num_of_leading):
        if text[i] == " ":
            text[i] = ""
        else:
            break

    for i in range(1, num_of_trailing+1):
        if text[-i] == " ":
            text[-i] = ""
        else:
            break
    return ''.join(text)

txt1 = "   MY name is     "
print(remove_outer_spaces(txt1, 1, 1))  # result is: "  MY name is    "
print(remove_outer_spaces(txt1, 2, 3))  # result is: " MY name is  "
print(remove_outer_spaces(txt1, 6, 8))  # result is: "MY name is"
Contractive answered 25/10, 2019 at 10:17 Comment(0)
J
0

How do I remove leading and trailing whitespace from a string in Python?

So below solution will remove leading and trailing whitespaces as well as intermediate whitespaces too. Like if you need to get a clear string values without multiple whitespaces.

>>> str_1 = '     Hello World'
>>> print(' '.join(str_1.split()))
Hello World
>>>
>>>
>>> str_2 = '     Hello      World'
>>> print(' '.join(str_2.split()))
Hello World
>>>
>>>
>>> str_3 = 'Hello World     '
>>> print(' '.join(str_3.split()))
Hello World
>>>
>>>
>>> str_4 = 'Hello      World     '
>>> print(' '.join(str_4.split()))
Hello World
>>>
>>>
>>> str_5 = '     Hello World     '
>>> print(' '.join(str_5.split()))
Hello World
>>>
>>>
>>> str_6 = '     Hello      World     '
>>> print(' '.join(str_6.split()))
Hello World
>>>
>>>
>>> str_7 = 'Hello World'
>>> print(' '.join(str_7.split()))
Hello World

As you can see this will remove all the multiple whitespace in the string(output is Hello World for all). Location doesn't matter. But if you really need leading and trailing whitespaces, then strip() would be find.

Jerkwater answered 17/5, 2020 at 0:42 Comment(0)
G
0

One way is to use the .strip() method (removing all surrounding whitespaces)

str = "  Hello World  "
str = str.strip()
**result: str = "Hello World"**

Note that .strip() returns a copy of the string and doesn't change the underline object (since strings are immutable).

Should you wish to remove all whitespace (not only trimming the edges):

str = ' abcd efgh ijk  '
str = str.replace(' ', '')
**result: str = 'abcdefghijk'
Gamic answered 7/5, 2022 at 7:15 Comment(0)
C
0

You can also use str.strip() as a function:

str.strip(" Hello world ")   # 'Hello world'

Same with str.lstrip() and str.rstrip():

str.lstrip(" hello ")        # 'hello '
str.rstrip(" hello ")        # ' hello'

This is useful if you need it a context where a callable is expected. For example, we can strip white space from strings in a list by mapping str.strip to a list:

lst = [" Hello ", " Hello", "Hello ", " Bob has a cat      "]
list(map(str.strip, lst))
# ['Hello', 'Hello', 'Hello', 'Bob has a cat']

Another example: pandas' str.strip is really slow but mapping Python's str.strip is on the columns is 2x faster, especially because we don't need to construct a lambda to use it:

pd.Series(lst).map(str.strip)
Chromogenic answered 10/12, 2023 at 3:40 Comment(0)
L
-1

I wanted to remove the too-much spaces in a string (also in between the string, not only in the beginning or end). I made this, because I don't know how to do it otherwise:

string = "Name : David         Account: 1234             Another thing: something  " 

ready = False
while ready == False:
    pos = string.find("  ")
    if pos != -1:
       string = string.replace("  "," ")
    else:
       ready = True
print(string)

This replaces double spaces in one space until you have no double spaces any more

Lenna answered 24/9, 2018 at 10:53 Comment(2)
Although this works, it's not very efficient, use this instead: https://mcmap.net/q/46368/-substitute-multiple-whitespace-with-single-whitespace-in-python-duplicateBouldin
if you want to remove all spaces just use string.replace(" ","") no need for all this codePriscian

© 2022 - 2025 — McMap. All rights reserved.