Remove last character if it's a backslash

PythonString

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

If you don't mind all trailing backslashes being removed, you can use string.rstrip()

For example:

x = '\\abc\\'
print x.rstrip('\\')

prints:

\abc

But there is a slight problem with this (based on how your question is worded): This will strip ALL trailing backslashes. If you really only want the LAST character to be stripped, you can do something like this:

if x[-1] == '\\': x = x[:-1]

Solution 3 - Python

If you only want to remove one backslash in the case of multiple, do something like:

s = s[:-1] if s.endswith('\\') else s

Solution 4 - Python

if s.endswith('\\'):
    s = s[:-1]

Solution 5 - Python

The rstrip function will remove more than just the last character, though. It will remove all backslashes from the end of the string. Here's a simple if statement that will remove just the last character:

if s[-1] == '\\':
	s = s[:-1]

Solution 6 - Python

Or not so beautiful(don't beat me) but also works:

stripSlash = lambda strVal: strVal[:-1] if strVal.endswith('\\') else strVal
stripSlash('sample string with slash\\')

And yes - rstrip is better. Just want to try.

Solution 7 - Python

We can use built-in function replace

my_str.replace(my_char,'')

my_chars = '\\'    
my_str = my_str.replace(my_char,'')

This will replace all \ in my_str

my_str.replace(my_chars, '',i)

my_char = '\\'
my_str = 'AB\CD\EF\GH\IJ\KL'
print ("Original my_str : "+ my_str)
for i in range(8):
    print ("Replace '\\' %s times" %(i))
    print("     Result : "+my_str.replace(my_chars, '',i))

This will replace \ in my_str i times Now you can control how many times you want to replace it with i

Output:

Original my_str : AB\CD\EF\GH\IJ\KL
Replace '\' 0 times
     Result : AB\CD\EF\GH\IJ\KL
Replace '\' 1 times
     Result : ABCD\EF\GH\IJ\KL
Replace '\' 2 times
     Result : ABCDEF\GH\IJ\KL
Replace '\' 3 times
     Result : ABCDEFGH\IJ\KL
Replace '\' 4 times
     Result : ABCDEFGHIJ\KL
Replace '\' 5 times
     Result : ABCDEFGHIJKL
Replace '\' 6 times
     Result : ABCDEFGHIJKL
Replace '\' 7 times
     Result : ABCDEFGHIJKL

Solution 8 - Python

For C# people who end up here:

my_string = my_string.TrimEnd('\\');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSergei BasharovView Question on Stackoverflow
Solution 1 - PythonFogleBirdView Answer on Stackoverflow
Solution 2 - PythonAdam BatkinView Answer on Stackoverflow
Solution 3 - PythonRob CowieView Answer on Stackoverflow
Solution 4 - PythonMatt JoinerView Answer on Stackoverflow
Solution 5 - PythonEdoDodoView Answer on Stackoverflow
Solution 6 - PythonArtsiom RudzenkaView Answer on Stackoverflow
Solution 7 - PythonAnuragChauhanView Answer on Stackoverflow
Solution 8 - PythonShadi NamroutiView Answer on Stackoverflow