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.
-
1What if there are multiple backslashes at the end of the string? Remove them all? – Felix Kling Jul 05 '11 at 15:11
8 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

- 74,300
- 25
- 125
- 131
-
23...and it will remove more than one character if there is more than one backslash at the end of the string. – Tim Pietzcker Jul 05 '11 at 15:10
-
@Tim: Good point... I guess the OP should clarify (from my understanding it should remove all the "special characters"). – Felix Kling Jul 05 '11 at 15:11
-
7If this is about normalizing path names, the OS-neutral way to do it is to use strip as said above, but the character to strip is os.sep -- that's backslash for Windows, forward slash for Unix. – Chris Johnson Oct 12 '12 at 20:24
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]

- 51,711
- 9
- 123
- 115
If you only want to remove one backslash in the case of multiple, do something like:
s = s[:-1] if s.endswith('\\') else s

- 22,259
- 6
- 62
- 56
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]

- 8,220
- 3
- 24
- 30
if s.endswith('\\'):
s = s[:-1]

- 112,946
- 110
- 377
- 526
-
1Did you try this? It's a syntax error. You need to use `if s.endswith('\\')` – Tim Pietzcker Jul 05 '11 at 15:17
-
1Seriously I run into that every time. What the hell is the point of raw strings if they don't work as expected. – Matt Joiner Jul 05 '11 at 15:22
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.

- 27,895
- 4
- 34
- 52
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

- 143
- 1
- 15
For C# people who end up here:
my_string = my_string.TrimEnd('\\');

- 11,796
- 4
- 56
- 54