Python – 将字符串转换为 unicode字符
给定一个字符串,将其字符转换为 Unicode字符。
Input : test_str = ‘gfg’
Output : \u0067\u0066\u0067
Explanation : Result changed to unicoded string.
Input : test_str = ‘himani’
Output : \u0068\u0069\u006D\u0061\u006E\u0069
Explanation : Result changed to unicoded string.
方法#1:使用 re.sub() + ord() + lambda
在此,我们使用 re.sub() 执行替换任务,并使用 lambda函数使用 ord() 执行每个字符的转换任务。
Python3
# Python3 code to demonstrate working of
# Convert String to unicode characters
# using re.sub() + ord() + lambda
import re
# initializing string
test_str = 'geeksforgeeks'
# printing original String
print("The original string is : " + str(test_str))
# using sub() to perform substitutions
# ord() for conversion.
res = (re.sub('.', lambda x: r'\u % 04X' % ord(x.group()), test_str))
# printing result
print("The unicode converted String : " + str(res))
Python3
# Python3 code to demonstrate working of
# Convert String to unicode characters
# using join() + format() + ord()
import re
# initializing string
test_str = 'geeksforgeeks'
# printing original String
print("The original string is : " + str(test_str))
# using format to perform required formatting
res = ''.join(r'\u{:04X}'.format(ord(chr)) for chr in test_str)
# printing result
print("The unicode converted String : " + str(res))
输出
The original string is : geeksforgeeks
The unicode converted String : \u0067\u0065\u0065\u006B\u0073\u0066\u006F\u0072\u0067\u0065\u0065\u006B\u0073
方法 #2:使用 join() + format() + ord()
在此,unicode 格式字符串的替换任务是使用 format() 完成的,而 ord() 用于转换。
Python3
# Python3 code to demonstrate working of
# Convert String to unicode characters
# using join() + format() + ord()
import re
# initializing string
test_str = 'geeksforgeeks'
# printing original String
print("The original string is : " + str(test_str))
# using format to perform required formatting
res = ''.join(r'\u{:04X}'.format(ord(chr)) for chr in test_str)
# printing result
print("The unicode converted String : " + str(res))
输出
The original string is : geeksforgeeks
The unicode converted String : \u0067\u0065\u0065\u006B\u0073\u0066\u006F\u0072\u0067\u0065\u0065\u006B\u0073