Remove specific strings at the end of python

When you want to delete a specific character string at the end

For a string S, if there is a string s at the end of S When you want to delete only the last s.

S = 'abc123abcabc'
s = 'abc'

if S.endswith(s):
    S = S[:-len(s)]

#Output result: abc123abc

You can also do the same with regular expressions.

import re

S = "abc123abcabc"
s = "abc$"

print(re.sub(search, "", string))

#Output result: abc123abc

This may be cleaner.

At first, I wrote how to use rstrip (), I completely misunderstood it, so I deleted it. Thank you for your comment.

Recommended Posts

Remove specific strings at the end of python
Python Basic Course (at the end of 15)
Send Gmail at the end of the process [Python]
Change the length of Python csv strings
python> print> Redirected only at the end of processing?> Run with -u
Tasks at the start of a new python project
Towards the retirement of Python2
About the ease of Python
About the features of Python
The Power of Pandas: Python
Get the number of specific elements in a python list
How to insert a specific process at the start and end of spider with scrapy
The story of Python and the story of NaN
[Python] The stumbling block of import
First Python 3 ~ The beginning of repetition ~
pyenv-change the python version of virtualenv
Change the Python version of Homebrew
[Python] Understanding the potential_field_planning of Python Robotics
Review of the basics of Python (FizzBuzz)
About the basics list of Python basics
Learn the basics of Python ① Beginners
Check the behavior of destructor in Python
What is the XX file at the root of a popular Python project?
[Python3] Understand the basics of Beautiful Soup
Pass the path of the imported python module
The story of making Python an exe
Learning notes from the beginning of Python 1
Check the existence of the file with python
About the virtual environment of python version 3.7
Shout Hello, Reiwa! At the beginning of Reiwa
[Python] Understand the content of error messages
Python> sys.path> List of strings indicating the path to search for modules
[Python3] Rewrite the code object of the function
The result of installing python in Anaconda
[Python] Try pydash of the Python version of lodash
[python] Checking the memory consumption of variables
Check the path of the Python imported module
The story of manipulating python global variables
[python] [meta] Is the type of python a type?
Call the python debugger at any time
The basics of running NoxPlayer in Python
Pandas of the beginner, by the beginner, for the beginner [Python]
Bulk replacement of strings in Python arrays
The Python project template I think of.
Try to react only the carbon at the end of the chain with SMARTS
In search of the fastest FizzBuzz in Python
Set the process name of the Python program
[Python] Get the character code of the file
The story of blackjack A processing (python)
Get a datetime instance at any time of the day in Python
Intuitively learn the reshape of Python np
Python Note: The secret role of commas
[Machine learning pictorial book] A memo when performing the Python exercise at the end of the book while checking the data
Python learning memo for machine learning by Chainer until the end of Chapter 2
Learning notes from the beginning of Python 2
Summarize the titles of Hottentori at the end and look at the present on the Web
Japanese translation: PEP 20 --The Zen of Python
[Python3] Understand the basics of file operations
Automation of creation of working hours table created at the end of the month with Selenium
About the matter that softmax is not needed at the end of Torchvision model.
Output the number of CPU cores in Python