Python Forum
How to replace on char with another in a string? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to replace on char with another in a string? (/thread-31290.html)



How to replace on char with another in a string? - korenron - Dec-02-2020

I need to replace one char in another in string

I have try this :
name = "you\me"

result=name.replace('\','/')
print(name)
print("******************")
print(result)
but I get this error
SyntaxError: EOL while scanning string litera
what can I do ?

Thanks ,


RE: How to replace on char with another in a string? - deanhystad - Dec-02-2020

\ is the escape character and is used to indicate he next character is special. For example, \n is a newline character and not '\' followed by 'n'. If you want a backslash as a character you use an escape backslash or '\\'


RE: How to replace on char with another in a string? - bowlofred - Dec-02-2020

The backslash is a special character. When you type '\', python thinks you're trying to put a single quote into a string, not terminate the string. To avoid this and have it insert a backslash, you need to double the backslash.

Change the line to:
result=name.replace('\\','/')



RE: How to replace on char with another in a string? - korenron - Dec-03-2020

thank you
I forgot about it ......