Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove last character if it's a backslash

Tags:

python

string

Is there a function to chomp last character in the string if it's some special character? For example, I need to remove backslash if it's there, and do nothing, if not. I know I can do it with regex easily, but wonder if there something like a small built-in function for that.

like image 586
Sergei Basharov Avatar asked Jul 05 '11 15:07

Sergei Basharov


People also ask

Does Rstrip remove newline?

rstrip('\n') . This will strip all newlines from the end of the string, not just one.

How do I remove backslash from a list in Python?

Using replace() Function to Remove Backslash from String in Python. The replace() can be utilized to remove an escape sequence or just a backslash in Python by simply substituting it with space in Python.

How do I get rid of char?

Use the deleteCharAt Method to Remove a Character From String in Java. The deleteCharAt() method is a member method of the StringBuilder class that can also be used to remove a character from a string in Java.


1 Answers

Use rstrip to strip the specified character(s) from the right side of the string.

my_string = my_string.rstrip('\\') 

See: http://docs.python.org/library/stdtypes.html#str.rstrip

like image 147
FogleBird Avatar answered Oct 06 '22 11:10

FogleBird