Remove space in string python
I want to eliminate all the whitespace from a string, on both ends, and in between words. I have this Python code:
But that only eliminates the whitespace on both sides of the string. How do I remove all whitespace? asked Nov 25, 2011 at 13:51
0x120x12 18.2k21 gold badges65 silver badges123 bronze badges 3 If you want to remove leading and ending spaces, use
If you want to remove all space characters, use
If you want to remove duplicated spaces, use
Mateen Ulhaq 22.2k16 gold badges86 silver badges127 bronze badges answered Nov 25, 2011 at 13:56
Cédric JulienCédric Julien 76.1k15 gold badges121 silver badges128 bronze badges 7 To remove only spaces use
To remove all whitespace characters (space, tab, newline, and so on) you can use
or a regular expression:
If you want to only remove whitespace from the
beginning and end you can use
You can also use
Randall Cook 6,5986 gold badges32 silver badges67 bronze badges answered Nov 25, 2011 at 13:54
Mark ByersMark Byers 781k184 gold badges1551 silver badges1440 bronze badges 2 An alternative is to use regular expressions and match these strange white-space characters too. Here are some examples: Remove ALL spaces in a string, even between words:
Remove spaces in the BEGINNING of a string:
Remove spaces in the END of a string:
Remove spaces both in the BEGINNING and in the END of a string:
Remove ONLY DUPLICATE spaces:
(All examples work in both Python 2 and Python 3) answered Feb 19, 2015 at 13:05
Emil StenströmEmil Stenström 12.3k8 gold badges49 silver badges73 bronze badges 3 "Whitespace" includes space, tabs, and CRLF. So an elegant and
one-liner string function we can use is Python 3
OR if you want to be thorough:
Python 2
OR if you want to be thorough:
ib. 26.7k10 gold badges77 silver badges99 bronze badges answered Nov 28, 2015 at 3:36
MaKMaK 1,6081 gold badge16 silver badges6 bronze badges 3 For removing whitespace from beginning and end, use
answered Nov 25, 2011 at 13:56
wal-o-matwal-o-mat 6,9487 gold badges30 silver badges40 bronze badges 2
MaK already pointed out the "translate" method above. And this variation works with Python 3 (see this Q&A).
Asclepius 52k15 gold badges150 silver badges131 bronze badges answered Sep 26, 2016 at 9:54
1 In addition, strip has some variations: Remove spaces in the BEGINNING and END of a string:
Remove spaces in the BEGINNING of a string:
Remove spaces in the END of a string:
All three string functions
Or you could remove extra commas when reading in a string list:
answered Apr 6, 2018 at 20:51
cacti5cacti5 1,8002 gold badges24 silver badges32 bronze badges Be careful:
If you only replace spaces and tabs you can end up with hidden CRLFs that appear to match what you are looking for, but are not the same.
answered Nov 12, 2014 at 19:30
yan bellavanceyan bellavance 4,58020 gold badges59 silver badges92 bronze badges 1 answered Mar 13, 2020 at 15:51
handlehandle 6,1443 gold badges49 silver badges76 bronze badges 1 I use split() to ignore all whitespaces and use join() to concatenate strings.
I prefer this approach because it is only a expression (not a statement).
answered Jul 29, 2021 at 14:33
naoki fujitanaoki fujita 6611 gold badge7 silver badges13 bronze badges
answered Oct 24, 2016 at 12:46
1 In the following script we import the regular expression module which we use to substitute one space or more with a single space. This ensures that the inner extra spaces are removed. Then we use strip() function to remove leading and trailing spaces.
answered Feb 19 at 10:59
2 I found that this works the best for me:
It removes any whitespaces, tabs, etc. answered Jul 25 at 10:08
try this.. instead of using re i think using split with strip is much better
answered Oct 10, 2020 at 19:36
Assad AliAssad Ali 2791 silver badge12 bronze badges |