So I can’t seem to figure this out… I have a string say, "a\nb" and I want this to become "anb". I’ve tried all the following and none seem to work;
>>> a
'a\nb'
>>> a.replace("\","")
File "<stdin>", line 1
a.replace("\","")
^
SyntaxError: EOL while scanning string literal
>>> a.replace("\",r"")
File "<stdin>", line 1
a.replace("\",r"")
^
SyntaxError: EOL while scanning string literal
>>> a.replace("\",r"\")
'a\\nb'
>>> a.replace("\","\")
'a\nb'
I really don’t understand why the last one works, because this works fine:
>>> a.replace("\","%")
'a%nb'
Is there something I’m missing here?
EDIT I understand that is an escape character. What I’m trying to do here is turn all \n \t etc. into n t etc. and replace doesn’t seem to be working the way I imagined it would.
>>> a = "a\nb"
>>> b = "anb"
>>> print a
anb
>>> print b
a
b
>>> a.replace("\","\")
'a\nb'
>>> a.replace("\\","\")
'a\nb'
I want string a to look like string b. But replace isn’t replacing slashes like I thought it would.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
There’s no need to use replace for this.
What you have is a encoded string (using the string_escape encoding) and you want to decode it:
>>> s = r"EscapednNewline"
>>> print s
EscapednNewline
>>> s.decode('string_escape')
'EscapednNewline'
>>> print s.decode('string_escape')
Escaped
Newline
>>> "a\nb".decode('string_escape')
'anb'
In Python 3:
>>> import codecs
>>> codecs.decode('\n\x21', 'unicode_escape')
'n!'
Method 2
You are missing, that is the escape character.
Look here: http://docs.python.org/reference/lexical_analysis.html
at 2.4.1 “Escape Sequence”
Most importantly n is a newline character.
And \ is an escaped escape character 😀
>>> a = 'a\\nb'
>>> a
'a\\nb'
>>> print a
a\nb
>>> a.replace('\\', '\')
'a\nb'
>>> print a.replace('\\', '\')
anb
Method 3
r'a\nb'.replace('\\', '\')
or
'anb'.replace('n', '\n')
Method 4
Your original string, a = 'a\nb' does not actually have two '' characters, the first one is an escape for the latter. If you do, print a, you’ll see that you actually have only one '' character.
>>> a = 'a\nb' >>> print a anb
If, however, what you mean is to interpret the 'n' as a newline character, without escaping the slash, then:
>>> b = a.replace('\n', 'n')
>>> b
'anb'
>>> print b
a
b
Method 5
It’s because, even in “raw” strings (=strings with an r before the starting quote(s)), an unescaped escape character cannot be the last character in the string. This should work instead:
'\ '[0]
Method 6
In Python string literals, backslash is an escape character. This is also true when the interactive prompt shows you the value of a string. It will give you the literal code representation of the string. Use the print statement to see what the string actually looks like.
This example shows the difference:
>>> '\' '\' >>> print '\'
Method 7
In Python 3 it will be:
bytes(s, 'utf-8').decode("unicode_escape")
Method 8
This works on Windows with Python 3.x:
import os str(filepath).replace(os.path.sep, '/')
Where: os.path.sep is on Windows and / on Linux.
Case study
Used this to prevent errors when generating a Markdown file then rendering it to pdf.
Method 9
path = "C:\Users\Programming\Downloads"
# Replace \ with a along with any random key multiple times
path.replace('\', 'pppyyyttthhhooonnn')
# Now replace pppyyyttthhhooonnn with a blank string
path.replace("pppyyyttthhhooonnn", "")
print(path)
#Output…
C:UsersProgrammingDownloads
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0