-3

I have to replace every '%' character with '%%' in a string and as string are immutable i am doing list(string) and then replace every '%' with '%%'. But string can be huge(len(string)>2000) and as a result list can be huge that can slow down so i want to know the fastest way to do that in python 2.7

which_part
  • 792
  • 3
  • 11
  • 26

1 Answers1

0

Why don't you just use replace method on string? Use the following code

a = "hello %"
print(a.replace("%","%%")

There is one more way to this, and that is to use Regex. but, go through This SO Post to know why string replace method is preferred over regex

rawwar
  • 4,834
  • 9
  • 32
  • 57