R has a handy function called chartr ()
.
This is a function that when you specify two strings, replace each of their corresponding characters.
Let's see an example.
R
txt <- "I like orange"
chartr("abcde", "ABCDE", txt)
result
[1] "I likE orAngE."
To do this in Python:
Python
import string
txt = "I like orange."
trans = string.maketrans("abcde", "ABCDE")
result = txt.translate(trans)
print(result)
result
I likE orAngE.
Enjoy!
-[R string processing function --RjpWiki](http://www.okadajp.org/RWiki/?R%E3%81%AE%E6%96%87%E5%AD%97%E5%88%97 % E5% 87% A6% E7% 90% 86% E9% 96% A2% E6% 95% B0) -Master string substitution in Python-orangain flavor
Recommended Posts