Convert a Unicode string to a string in Python (containing extra symbols)
You can use the .encode() method to convert a Unicode string to a string containing extra symbols in Python.
You can use the .encode() method to convert a Unicode string to a bytes object in Python. Here is an example:
Convert a Unicode string to a bytes object in Python
python— editable, runs on the server
This will output a bytes object: b'Hello, \xe4\xb8\x96\xe7\x95\x8c!'
You can also use the .decode() method to convert the bytes back to a str object:
Using decode() method in Python
original_string = encoded_bytes.decode("utf-8")
print(original_string)This will output Hello, 世界!
Note that the encoding format passed to the .encode() and .decode() methods should be the same.