Text Strip() now working constantly.

Chris Angelico rosuav at gmail.com
Sat Jan 27 13:25:42 EST 2018


On Sun, Jan 28, 2018 at 4:25 AM, George Shen <gjshen at gmail.com> wrote:
> Hi Python Group,
>
> I am not sure if I am doing this correctly however, I believe I found a bug
> that involves the striping of a text string.

If you're implying that it's a bug in Python, no it isn't. If you're
trying to understand the bug in your own code, you've come to the
right place :)

> I have attached a JPG that clearly illustrate the issue.

Attachments aren't carried on this list; text is better anyway.

> I am currently using 2.7.13
>
> In short:
> Given a string.
> 'cm_text.data'
> if you try and strip '.data'
> the return string will be
> 'cm_tex'

Correct. In fact, the strip function does not remove a substring; it
removes a set of characters. It's most commonly used for removing any
whitespace from the ends of a string. It faithfully removed every ".",
"d", "a", and "t" from your string - including the one at the end of
"text".

If you know for sure that the name WILL end with ".data", you can
simply ask Python to remove the last five characters:

>>> 'cm_text.data'[:-5]
'cm_text'

That may better suit what you're doing.

By the way, you may wish to consider migrating to Python 3, as Python
2.7 is a decade old and not getting any further enhancements.

ChrisA



More information about the Python-list mailing list