St4rb0y
St4rb0y

Reputation: 309

Replacing one percent symbol with two in Python?

I'm trying to clean up strings containing percent symbols. What I intend do is replace each % with %%!

When I use my_string.replace("%", "%%"), it works out fine in Python 3. However, when I do the same in Python 2.7 and print the result (using the future print function), it simply prints the original string. I guess this happens, because %% is commonly used to escape the percent symbol in strings?

What would be a viable workaround for Python 2.7? I'm parsing FFmpeg commands, and single percent symbols are forbidden, since FFmpeg uses %dto indentify number sequences in filenames of image sequences.

Thanks.

Upvotes: 0

Views: 816

Answers (1)

ivan .B
ivan .B

Reputation: 96

Using python 2.7 this is an example to replace % by %%:

import string
str = "replace % "
str = string.replace(str, "%","%%")
print "Result:",str

The result is:

Result: replace %% 

Upvotes: 1

Related Questions