Python Strip

All things Programming
Post Reply
User avatar
dont_think_twice
Extreme Unicyclist
Posts: 820
Joined: Sat Dec 19, 2020 4:15 am

Python Strip

Post by dont_think_twice »

Code: Select all

>>> t = "win2.s2p"
>>> t
'win2.s2p'
>>> t.strip(".s2p")
'win'
What am I missing?
User avatar
e3k
Director of Sandbags
Posts: 541
Joined: Fri Dec 18, 2020 8:19 am
Location: house of rising sun
Contact:

Re: Python Strip

Post by e3k »

>>> t = "win2.s2p"
>>> t
'win2.s2p'
>>> t.strip("p2.s")
'win'
#controlgroup
...71...
Director of Sandbags
Posts: 588
Joined: Sat Dec 19, 2020 8:49 am

Re: Python Strip

Post by ...71... »

dont_think_twice wrote: Sat Feb 12, 2022 6:27 am

Code: Select all

>>> t = "win2.s2p"
>>> t
'win2.s2p'
>>> t.strip(".s2p")
'win'
What am I missing?
The order does not matter.

Code: Select all

In [7]: t.strip('2sp.n')
Out[7]: 'wi'
...71...
Director of Sandbags
Posts: 588
Joined: Sat Dec 19, 2020 8:49 am

Re: Python Strip

Post by ...71... »

OFFTOP: Here's a good joke from python world:

Code: Select all

In [1]: a = 1234
In [2]: b = 1234

In [3]: a is b
Out[3]: False

In [4]: a = 115
In [5]: b = 115

In [6]: a is b
Out[6]: True
User avatar
RoGeorge
National Metrics Strategist
Posts: 484
Joined: Sat Dec 19, 2020 4:47 am

Re: Python Strip

Post by RoGeorge »

LOL, I was not aware about that. :D

https://stackoverflow.com/questions/136 ... s-operator
Python is allowed to automatically intern any immutable types, but not required to do so. Different implementations will intern different values.

CPython (the implementation you're using if you don't know which implementation you're using) auto-interns small integers and some special singletons like False, but not strings (or large integers, or small tuples, or anything else).
User avatar
RoGeorge
National Metrics Strategist
Posts: 484
Joined: Sat Dec 19, 2020 4:47 am

Re: Python Strip

Post by RoGeorge »

What's even stranger is the range of integers for which a is b: [-5 to +256] inclusive. :P
User avatar
Naib
Site Admin
Posts: 1105
Joined: Sat Dec 19, 2020 2:20 am

Re: Python Strip

Post by Naib »

dont_think_twice wrote: Sat Feb 12, 2022 6:27 am

Code: Select all

>>> t = "win2.s2p"
>>> t
'win2.s2p'
>>> t.strip(".s2p")
'win'
What am I missing?
As others have pointed out "".strip(...) strips all occurances of the characters.
Pre 3.9 what you want todo is txt.split('.')[0] and since 3.9 you can use the removesuffix() method
The best argument against democracy is a five-minute conversation with the average voter

Great Britain is a republic, with a hereditary president, while the United States is a monarchy with an elective king
User avatar
e3k
Director of Sandbags
Posts: 541
Joined: Fri Dec 18, 2020 8:19 am
Location: house of rising sun
Contact:

Re: Python Strip

Post by e3k »

yes i would also go with split Naib. did know there is a remove suffix in 3.9.
#controlgroup
User avatar
dont_think_twice
Extreme Unicyclist
Posts: 820
Joined: Sat Dec 19, 2020 4:15 am

Re: Python Strip

Post by dont_think_twice »

Thanks everyone. I switched to split last night without understanding why the strip didn’t work. Amazing that I never understood how that method worked.
User avatar
RoGeorge
National Metrics Strategist
Posts: 484
Joined: Sat Dec 19, 2020 4:47 am

Re: Python Strip

Post by RoGeorge »




Never trust a Python stripper!


Code: Select all

$ python3
Python 3.8.10 (default, Nov 26 2021, 20:14:08) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> girl = "Dana"
>>> girl.strip("all clothes")
'Dan'
:lol:
Ralphred
Chief of Unicorn Division
Posts: 187
Joined: Tue May 11, 2021 4:22 pm

Re: Python Strip

Post by Ralphred »

dont_think_twice wrote: Sat Feb 12, 2022 6:41 pmI switched to split last night without understanding why the strip didn’t work.

Code: Select all

''.join(t.split('.s2p'))
Seems a bit overkill, but if your not running it thousands of times a second, and won't have ".suffix" anywhere else in a string it'll work. Personally I'd be using if t.endswith() and t[:-4], it lends itself to expansion to work with multiple suffixes and non-iterative matching with little tweaking...
Were Dante alive today, he would agree that the tenth circle of hell is reserved for people who have non-ANSII characters in online usernames.
User avatar
notageek
Site Admin
Posts: 757
Joined: Fri Dec 18, 2020 2:28 am
Location: India

Re: Python Strip

Post by notageek »

Code: Select all

>>> t = "win2.s2p"
>>> t.replace(".s2p","")
'win2'

Code: Select all

>>> t = "win2.s2p"
>>> t[:-4]
'win2'
"Defeat is a state of mind. No one is ever defeated, until defeat has been accepted as a reality." -- Bruce Lee
Post Reply