0

How do I place a variable in .replace() in python. For example:

x = 2

example = example2.replace("1", x)

I think its clear what I am looking for, I just have no clue how to do it.

Håken Lid
  • 22,318
  • 9
  • 52
  • 67

1 Answers1

0

My comments are based on Python v2.7:

In your code, "x" is assigned a integer value, they do not have replace method. Instead, if "x" had carried normal string, then replace is available and this is how it works:

var2 = "palindrome syndrome"
print var2.replace("drome", "pal", 2)

Output:

palinpal synpal

In the statement, var2.replace, there are three arguments: "drome" is the substring to find and replace with new string "pal" and do this for two occurrences of "drome".

Ramu
  • 3
  • 1
  • 5