Home > Enterprise >  Replacing a String in python using replace
Replacing a String in python using replace

Time:07-18

I have a sentence with "Hello World/"

I need to replace "/" with a "\". I am using python replace method but it throws an error

var = "Hello World/"
vav2 = var.replace("/" , "\")

CodePudding user response:

var = "Hello World/" 
vav2 = var[:-1]

I try vav2 = var.replace("/" , "\\"),but it not work as I expect

CodePudding user response:

Try doing:

var = "Hello World/"    
var.replace('/','\\')

For replacing multilple strings with a loop you can do something like:

for r in (("/", "\\"), (",", "\\")):
    var = var.replace(*r)
  • Related