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
Asked
Active
Viewed 1,295 times
-3

which_part
- 792
- 3
- 11
- 26
-
2use `some_var.replace('%', '%%')` function – Mauricio Cortazar Jul 18 '18 at 01:27
-
Strings are immutable in Python. Your best bet is `.replace` – dawg Jul 18 '18 at 01:29
1 Answers
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