Python Forum
How to replace on char with another in a string?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to replace on char with another in a string?
#1
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 ,
Reply
#2
\ 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 '\\'
korenron likes this post
Reply
#3
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('\\','/')
korenron likes this post
Reply
#4
thank you
I forgot about it ......
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to replace a string with a file (HTML file) tester_V 1 762 Aug-30-2023, 03:42 AM
Last Post: Larz60+
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,122 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  Replace string in a nested Dictianory. SpongeB0B 2 1,196 Mar-24-2023, 05:09 PM
Last Post: SpongeB0B
  Replace with upper(string) WJSwan 7 1,584 Feb-10-2023, 10:28 AM
Last Post: WJSwan
  Find and Replace numbers in String giddyhead 2 1,222 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,104 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
  Replace String with increasing numer [SOLVED] AlphaInc 13 5,010 Aug-07-2021, 08:16 AM
Last Post: perfringo
  How to remove char from string?? ridgerunnersjw 2 2,544 Sep-30-2020, 03:49 PM
Last Post: ridgerunnersjw
  Sum char word dictionary RavCOder 3 2,344 Nov-08-2019, 01:32 PM
Last Post: perfringo
  Replace string in many files in a folder metro17 8 5,587 Oct-16-2019, 06:46 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020