[PYTHON] A story that failed when trying to remove the suffix from the string with rstrip

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

A story that failed when trying to remove the suffix from the string with rstrip
A story that suffered from OS differences when trying to implement a dissertation
A story that required preparation when trying to do a Django tutorial with plain centos7
A story that got stuck when trying to upgrade the Python version on GCE
A program that failed when trying to create a linebot with reference to "Dialogue system made with python"
A story that didn't work when I tried to log in with the Python requests module
A story that I was addicted to when I made SFTP communication with python
A story that struggled with the common set HTTP_PROXY = ~
A memo of misunderstanding when trying to load the entire self-made module with Python3
[python] A note when trying to use numpy with Cython
A story about how to deal with the CORS problem
The story that the private key is set to 600 with chmod
Django-A story that got stuck when trying to operate mail with SES and Amazon SES (also introduces the fastest operation procedure)
When writing to a csv file with python, a story that I made a mistake and did not meet the delivery date
A story that I was addicted to calling Lambda from AWS Lambda.
A story that I had a hard time trying to create an "app that converts images like paintings" with the first web application
I want to use a wildcard that I want to shell with Python remove
A story about trying to automate a chot when cooking for yourself
[Introduction to Python] How to split a character string with the split function
The story that a hash error came out when using Pipenv
A story that I fixed when I got Lambda logs from Cloudwatch Logs
A story that struggled to handle the Python package of PocketSphinx
Try to extract a character string from an image with Python3
How to deal with the problem that the current directory moves when Python is executed from Atom
How to extract the desired character string from a line 4 commands
The story that the guard was confined when the laboratory was converted to IoT
How to deal with the error "Failed to load module" canberra-gtk-module "that appears when you run OpenCV
The story of the algorithm drawing a ridiculous conclusion when trying to solve the traveling salesman problem properly
The story of making a module that skips mail with python
About the error I encountered when trying to use Adafruit_DHT from Python on a Raspberry Pi
A story about creating a program that will increase the number of Instagram followers from 0 to 700 in a week
A story that visualizes the present of Qiita with Qiita API + Elasticsearch + Kibana
A story I was addicted to when inserting from Python to a PostgreSQL table
Extract lines that match the conditions from a text file with python
A story I was addicted to trying to get a video url with tweepy
[Python / Pandas] A bug occurs when trying to replace a DataFrame with `None` with` replace`
I get a UnicodeDecodeError when trying to connect to oracle with python sqlalchemy
[Introduction to Python] How to write a character string with the format function
The story of trying to push SSH_AUTH_SOCK obsolete on screen with LD_PRELOAD
The story that had nothing to do with partitions when I did disk backup with dd for the first time
A story that did not end with exit when turning while with pipe input
The story of IPv6 address that I want to keep at a minimum
I failed to install django with pip, so a reminder of the solution
Python version (PHP to Python) that deletes the subsequent character string from the specified character string (extension)
The story of making a web application that records extensive reading with Django
SSH login to the target server from Windows with a click of a shortcut
[Python] Solution to the problem that elements are linked when copying a list
[Python] I tried to get the type name as a string from the type function
A story of a high school graduate technician trying to predict the survival of the Titanic
Try to find the probability that it is a multiple of 3 and not a multiple of 5 when one is removed from a card with natural numbers 1 to 100 using Ruby and Python.
A model that identifies the guitar with fast.ai
How to create a function object from a string
Story of trying to use tensorboard with pytorch
Python Note: When assigning a value to a string
Save the object to a file with pickle
The story of moving from Pipenv to Poetry
A solution to the problem that kernel restarting frequently occurs when running PyQt system with jupyter or Spyder IDE
The story that fits in with pip installation
What failed when going from Javaer to Pythonista
It is a piggybacking story about the service that returns "Nyan" when you ping
Convert images from FlyCapture SDK to a form that can be used with openCV