Chris J. Vargo
Chris J. Vargo

Reputation: 2436

Escape Sequence for Backslash not working

Let's say we want our final output to say roll tide:

alabama = "roll\ tide"

This doesn't work because we need to escape the literal:

>>>print(laura.strip('\')

File "<stdin>", line 1
print(laura.strip('\')
SyntaxError: EOL while scanning string literal

So I do, using '\\' as instructed, but it still does this:

>>>print(laura.strip('\\'))    
roll\ tide

Upvotes: 0

Views: 453

Answers (3)

Adam Smith
Adam Smith

Reputation: 54163

str.strip only works at the beginning and ends of strings.

alabama = "\\roll\\ tide\\"
alamaba.strip("\\") # "roll\\ tide"

Instead use str.replace and replace it with nothing.

alabama = "roll\\ tide"
alabama.replace("\\", "") # "roll tide"

Upvotes: 0

Hackaholic
Hackaholic

Reputation: 19733

i think u need split:

>>> alabama.split("\\")
['roll', ' tide']
>>> "".join(alabama.split("\\"))
'roll tide'

strip only remove from front or end of string

if '\' at was end:

>>> alabama = "roll tide\\"
>>> alabama.strip("\\")
'roll tide'

you can also use re.sub:

>>> import re
>>> re.sub(r"\\","",alabama)
'roll tide'

you can also use str.replace:

>>> alabama.replace("\\","")
'roll tide'

Upvotes: 2

jrel
jrel

Reputation: 317

Try to replace it instead:

print(alabama.replace('\\',''))

Upvotes: 2

Related Questions