Delete a particular character in Python if it is the last

It is a memo

When you want to separate the output of TCPdump with a character, you may want to delete it if the last character of the IP is ":". ~~ At that time ~~

laststring.py


str="1234:5678:abcd:231a:"

if(str[-1:]==":"):
    print(str[:-1])
else:
    print(str)

You can go ~~

Postscript

This one was better

str="1234:5678:abcd:231a:"
print(str.rstrip(':'))

Recommended Posts

Delete a particular character in Python if it is the last
Check if the string is a number in python
[Golang] Check if a specific character string is included in the character string
In bash, "Delete the file if it exists".
In the python dictionary, if a non-existent key is accessed, initialize it with an arbitrary value
Check if it is Unix in the scripting language
Check if it is Unix in the scripting language
How to input a character string in Python and output it as it is or in the opposite direction.
What does the last () in a function mean in Python?
How to check in Python if one of the elements of a list is in another list
How to get the last (last) value in a list in Python
Is it a character string operation?
Python will fail if there is a space after the backslash
Play a sound in Python assuming that the keyboard is a piano keyboard
Write the test in a python docstring
Check if the URL exists in Python
Run the Python interpreter in a script
What is "mahjong" in the Python library? ??
Hash in Perl is a dictionary in Python
[python] [meta] Is the type of python a type?
A simple reason why the return value of round (2.675,2) is 2.67 in python (it should be 2.68 in reality ...)
What to do if there is a decimal in python json .dumps
If you want a singleton in python, think of the module as a singleton
If the conda environment is strange, let's put it back in mac
What is the fastest way to create a reverse dictionary in python?
[Python] Get the files in a folder with Python
Check if the characters are similar in Python
Get the caller of a function in Python
What is wheezy in the Docker Python image?
Make a copy of the list in Python
About the difference between "==" and "is" in python
Output in the form of a python array
[Python] Find the transposed matrix in a comprehension
Judge whether it is a prime number [Python]
python xlwings: Find the cell in the last row
Even if the development language is changed to python3 in Cloud9, version 2 is displayed in python --version
Various ways to read the last line of a csv file in Python
A solution if you accidentally remove the python interpreter in / usr / local / bin /.
If branch depending on whether there is a specific element in the list
If you guys in the scope kitchen can do it with a margin ~ ♪
[For beginners] Unexpected behavior if "\" is included when setting the path in Python
I created a script to check if English is entered in the specified position of the JSON file in Python.
If you define a method in a Ruby class and define a method in it, it becomes a method of the original class.
Create a python script to check if the link at the specified URL is valid 2
The result of making a map album of Italy honeymoon in Python and sharing it
Create a python script to check if the link at the specified URL is valid
[Python] How to expand variables in a character string
Try searching for a million character profile in Python
How to use any or all to check if it is in a dictionary (Hash)
[Selenium] Open the link in a new tab and move it [Python / Chrome Driver]
[Python] Leave only the elements that start with a specific character string in the array
Find the part that is 575 from Wikipedia in Python
What to do if the progress bar is not displayed in tqdm of python
If you encounter a "Unicode Decode Error" in Python
Determine if an attribute is defined in the object
python> check NoneType or not> if a == None:> if a is None:
Write a log-scale histogram on the x-axis in python
[Python] Precautions when it does not work even if TimedRotatingFileHandler is set in basicConfig in python2
Understand Python yield If you put yield in a function, it will change to a generator
A reminder about the implementation of recommendations in Python
[Python] Make sure the received function is a user-defined function