In rstrip, I wanted to do something like scraping if it matches the specified suffix.
rstrip.py
'012345_1.jpg'.rstrip('_1.jpg') #Expected value:012345 Result: 012345
'012345_9.jpg'.rstrip('_1.jpg') #Expected value: 012345_9.jpg result: 012345_9
'012345_11.jpg'.rstrip('_1.jpg') #Expected value: 012345_11.jpg result: 012345
As a result of worrying for an hour, it was written properly in the document.
The chars argument is not a suffix; rather, all combinations of its values are stripped 5. Built-in Types — Python v2.7 documentation
I thought I could write it cool, but in the end I responded with replace.
replace.py
'012345_1.jpg'.replace('_1.jpg', '') #Expected value:012345 Result: 012345
'012345_9.jpg'.replace('_1.jpg', '') #Expected value: 012345_9.jpg result: 012345_9.jpg
'012345_11.jpg'.replace('_1.jpg', '') #Expected value: 012345_11.jpg result: 012345_11.jpg
Recommended Posts