Notes on using rstrip with python.

What is rstrip

A convenient function rstrip that removes strings from the right. I used it as follows.

x = 'abcdefg'
x.rstrip('efg')
print(x)

Then the output is

abcd

Will be returned.

The mistake I noticed this time

This time, I was writing a program that deletes the "_1" after the one with "_1" from the elements of the list, but in places other than "_1" such as "101_1" and "141_1", "1" I noticed that there was an error in the part containing'. After all, when I wondered and investigated, it seems that rstrip does not delete only the argument ('1'), but also deletes only'' or '1'. In other words, the above example is as follows.

x = 'abcdefggefgfegef'
x.rstrip('efg')
print(x)

Then the output does not change

abcd

Use replace instead.

For example, in the program I wrote this time, I can modify it as follows.

before

list = ['101_1', '102_1', '102_2', '103_1']
list = [x.rstrip('_1') for x in list]

after

list = ['101_1', '102_1', '102_2', '103_1']
list = [x.replace('_1','') for x in list]

Of course this is not exact. I did this under the assumption that _1 is attached only at the end, but if you are curious, use a regular expression.

Recommended Posts

Notes on using rstrip with python.
Notes on installing Python using PyEnv
Notes for using OpenCV on Windows10 Python 3.8.3.
Notes on doing Japanese OCR with Python
Notes on using code formatter in Python
Notes using Python subprocesses
Notes on using Alembic
Notes on installing Python3 and using pip on Windows7
Notes on using dict in python [Competition Pro]
[Python] Notes on accelerating genetic algorithms using multiprocessing
Python notes using perl-ternary operator
[S3] CRUD with S3 using Python [Python]
Using Quaternion with Python ~ numpy-quaternion ~
Python notes using perl-special variables
[Python] Using OpenCV with Python (Basic)
[Django] Notes on using django-debug-toolbar
[Python] Notes on data analysis
Notes on optimization using Pytorch
Notes on installing Python on Mac
Broadcast on LINE using python
Notes on installing Python on CentOS
Using OpenCV with Python @Mac
Send using Python with Gmail
Notes on HDR and RAW image processing with Python
Troublesome story when using Python3 with VScode on ubuntu
Minimum notes when using Python on Mac (Homebrew edition)
Complement python with emacs using company-jedi
Harmonic mean with Python Harmonic mean (using SciPy)
Notes on Python and dictionary types
[Python] Using OpenCV with Python (Image Filtering)
Notes on package management with conda
Using Rstan from Python with PypeR
[Python] Using OpenCV with Python (Image transformation)
Notes on importing data from MySQL or CSV with Python
Introducing Python using pyenv on Ubuntu 20.04
[Python] Using OpenCV with Python (Edge Detection)
Preparing python using vscode on ubuntu
Notes on handling large amounts of data with python + pandas
IP spoof using tor on macOS and check with python
Notes on accessing dashDB from python
Install Python on CentOS using Pyenv
Study on Tokyo Rent Using Python (3-3)
Notes on deploying pyenv with Homebrew and managing Python versions
Getting started with Python 3.8 on Windows
Notes on using matplotlib on the server
When using MeCab with virtualenv python
Install Python on CentOS using pyenv
Precautions when using six with Python 2.5
(Beginner) Notes on using pyenv on Mac
[Memo] Tweet on twitter with python
How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~
Control the motor with a motor driver using python on Raspberry Pi 3!
Notes using cChardet and python3-chardet in Python 3.3.1.
WEB scraping with Python (for personal notes)
Run servo with Python on ESP32 (Windows)
Execute Python code on C ++ (using Boost.Python)
Try mathematical formulas using Σ with python
Notes on running M5Stick V with uPyLoader
Behind the flyer: Using Docker with Python
Notes on nfc.ContactlessFrontend () for nfcpy in python
Using Python and MeCab with Azure Databricks