I have an array of strings which looks like:
["U0001f308", "U0001F602"]
I need to add “\” in front of the first letter U so the output will be like:
["\U0001f308", "\U0001F602"]
This is the code I have tried so far:
matches = ["U0001f308", "U0001F602"]
emojis = [emoji.replace('U', r"\U") for emoji in matches]
print(emojis) #this prints ['\\U0001f308', '\\U0001F602'] which has two blacklashes
How can i add only one backslash in front of every string?
I guess what you want is the following code:
matches = ["U0001f308", "U0001F602"]
emojis = [emoji.replace('U', r"\U").encode().decode('unicode-escape') for emoji in matches]
print(emojis)
which prints
['🌈', '😂']
It's the same result as when we execute the following code:
print(["\U0001f308", "\U0001F602"])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.