Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't strip remove the space in this string?

Tags:

python

s = '种草 ​'
print(len(s))
s = s.strip()
print(len(s))

And the output for both is '4'. It seems the space takes up 2 characters and can't be removed by the strip() function. It's a Chinese space and can't be removed by the strip function.

like image 963
ling Avatar asked Dec 15 '25 03:12

ling


2 Answers

It's not a usual unicode space. you can remove it like this.

s = '种草 ​'
print(len(s))
s = s.strip(u'\u200b').strip()
print(len(s))
like image 132
Ahmad Farhan Avatar answered Dec 16 '25 20:12

Ahmad Farhan


strip removes spaces from both ends of a string.

>>> s = '种草 ​'
>>> ord(s[-1])
8203
>>> ord(s[-2])
32
>>> ord(' ')
32

The last character here is not a space character. The second last character is a space.

like image 40
Diptangsu Goswami Avatar answered Dec 16 '25 20:12

Diptangsu Goswami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!