Python – Convert String to unicode characters
Given a String, convert its characters to unicode characters.
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.
Method #1 : Using re.sub() + ord() + lambda
In this, we perform the task of substitution using re.sub() and lambda function is used to perform the task of conversion of each characters using ord().
Python3
import re
test_str = 'geeksforgeeks'
print ( "The original string is : " + str (test_str))
res = (re.sub( '.' , lambda x: r '\u % 04X' % ord (x.group()), test_str))
print ( "The unicode converted String : " + str (res))
|
Output
The original string is : geeksforgeeks
The unicode converted String : \u0067\u0065\u0065\u006B\u0073\u0066\u006F\u0072\u0067\u0065\u0065\u006B\u0073
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using join() + format() + ord()
In this, task of substitution in unicode formatted string is done using format() and ord() is used for conversion.
Python3
import re
test_str = 'geeksforgeeks'
print ( "The original string is : " + str (test_str))
res = ' '.join(r' \u{: 04X }'. format ( ord ( chr )) for chr in test_str)
print ( "The unicode converted String : " + str (res))
|
Output
The original string is : geeksforgeeks
The unicode converted String : \u0067\u0065\u0065\u006B\u0073\u0066\u006F\u0072\u0067\u0065\u0065\u006B\u0073
Time Complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
20 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...