ling
ling

Reputation: 1675

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

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.

Upvotes: 2

Views: 426

Answers (3)

Olvin Roght
Olvin Roght

Reputation: 7812

Probably, it's not well-looking solution, but it will strip all non-printable and whitespace characters:

from itertools import takewhile

check = lambda c: not c.isprintable() or c.isspace()

result = s[len(tuple(takewhile(check, s))): -len(tuple(takewhile(check, reversed(s))))]
# OR to not waste memory for tuple
result = s[sum(1 for _ in takewhile(check, s)): -sum(1 for _ in takewhile(check, reversed(s)))]

Here I used itertools.takewhile() to get all non-printable (str.isprintable()) and space (str.isspace()) chars from the start of string. Then using len() (or sum() in option with list comprehension) i got amount of such as chars and used this amount as start index in string slice. To get end index I used same method, but with reversed (reversed()) string.

Upvotes: 0

Ahmad Farhan
Ahmad Farhan

Reputation: 605

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))

Upvotes: 6

Diptangsu Goswami
Diptangsu Goswami

Reputation: 5965

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.

Upvotes: 5

Related Questions