In Python 2

>>> plain_string = "Hi!"
>>> unicode_string = u"Hi!"
>>> type(plain_string), type(unicode_string)
(<type 'str'>, <type 'unicode'>)

^ This is the difference between a byte string (plain_string) and a unicode string.

>>> s = "Hello!"
>>> u = unicode(s, "utf-8")

^ Converting to unicode and specifying the encoding.

In Python 3

All strings are unicode. The unicode function does not exist anymore. See answer from @Noumenon

Answer from user225312 on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-a-string-to-utf-8-in-python
Convert a String to Utf-8 in Python - GeeksforGeeks
July 23, 2025 - Converting a string to UTF-8 in Python is a simple task with multiple methods at your disposal. Whether you choose the encode method, the bytes constructor, or the str.encode method, the key is to specify the UTF-8 encoding.
🌐
O'Reilly
oreilly.com › library › view › python-cookbook › 0596001673 › ch03s18.html
Converting Between Unicode and Plain Strings - Python Cookbook [Book]
July 19, 2002 - # Convert Unicode to plain Python string: "encode" unicodestring = u"Hello world" utf8string = unicodestring.encode("utf-8") asciistring = unicodestring.encode("ascii") isostring = unicodestring.encode("ISO-8859-1") utf16string = unicodestring.encode("utf-16") # Convert plain Python string to Unicode: "decode" plainstring1 = unicode(utf8string, "utf-8") plainstring2 = unicode(asciistring, "ascii") plainstring3 = unicode(isostring, "ISO-8859-1") plainstring4 = unicode(utf16string, "utf-16") assert plainstring1==plainstring2==plainstring3==plainstring4 ·
Authors: Alex MartelliDavid Ascher
Published: 2002
Pages: 608
🌐
Python documentation
docs.python.org › 3 › howto › unicode.html
Unicode HOWTO — Python 3.14.7 documentation
This method takes an encoding argument, such as UTF-8, and optionally an errors argument. The errors argument specifies the response when the input string can’t be converted according to the encoding’s rules. Legal values for this argument are 'strict' (raise a UnicodeDecodeError exception), 'replace' (use U+FFFD, REPLACEMENT CHARACTER), 'ignore' (just leave the character out of the Unicode result), or 'backslashreplace' (inserts a \xNN escape sequence).
🌐
Medium
medium.com › @nawazmohtashim › method-to-encode-a-string-to-utf-8-in-python-b287027b7be9
Method to Encode a String to UTF-8 in Python | by Mohd Mohtashim Nawaz | Medium
February 2, 2024 - While encoding a string to UTF-8, it’s essential to consider potential errors that may arise. The encode() method allows you to handle errors by specifying the errors parameter. Common error handling options include: 'strict': Raises a UnicodeEncodeError if an error occurs (default behavior).
🌐
Java2Blog
java2blog.com › home › python › python string › encode string to utf-8 in python
Encode String to UTF-8 in Python [2 ways] - Java2Blog
December 25, 2022 - The same is not the case for Python 2. In this version, bytes and string are basically the same thing. So this, function is redundant since the string is already encoded. We can observe the same in the code below. ... To encode string to UTF-8 in Python, use the codecs.encode() function.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › convert-unicode-string-to-a-string-in-python
Convert Unicode String to a Byte String in Python - GeeksforGeeks
January 30, 2024 - The resulting `byte_string_bytes` represents the UTF-8 encoded byte sequence of the mixed-language Unicode string.
🌐
Evanjones
evanjones.ca › python-utf8.html
How to Use UTF-8 with Python (evanjones.ca)
Now, the byte string s will be treated as a sequence of UTF-8 bytes to create the Unicode string u. The next line stores the UTF-8 representation of u in the byte string backToBytes. Thankfully, everything in Python is supposed to treat Unicode strings identically to byte strings.
🌐
Python Guides
pythonguides.com › convert-string-to-utf-8-in-python
How To Convert String To UTF-8 In Python
May 16, 2025 - Learn how to convert a string to UTF-8 in Python using built-in methods like `encode()` for proper encoding, data storage, and cross-platform compatibility.
🌐
Real Python
realpython.com › python-encodings-guide
Unicode & Character Encodings in Python: A Painless Guide – Real Python
May 20, 2019 - Python 3 source code is assumed to be UTF-8 by default. This means that you don’t need # -*- coding: UTF-8 -*- at the top of .py files in Python 3. All text (str) is Unicode by default. Encoded Unicode text is represented as binary data (bytes).
🌐
Programiz
programiz.com › python-programming › methods › string › encode
Python String encode()
# unicode string string = 'pythön!' # print string print('The string is:', string) # default encoding to utf-8 · string_utf = string.encode() # print result print('The encoded version is:', string_utf) ... The string is: pythön! The encoded version (with ignore) is: b'pythn!' The encoded version (with replace) is: b'pyth?n!' Note: Try different encoding and error parameters as well. Since Python 3.0, strings are stored as Unicode, i.e.
🌐
W3Schools
w3schools.com › python › ref_string_encode.asp
Python String encode() Method
Python Examples Python Compiler ... Python Study Plan Python Interview Q&A Python Training ... The encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used....
🌐
Finxter
blog.finxter.com › home › learn python blog › python convert unicode to bytes, ascii, utf-8, raw string
Python Convert Unicode to Bytes, ASCII, UTF-8, Raw String - Be on the Right Side of Change
June 30, 2021 - Due to the fact that UTF-8 encoding ... For this task, both of the above methods are applicable. With encode(), we first get a byte string by applying UTF-8 encoding to the input Unicode string, and then use decode(), which will give us a UTF-8 encoded Unicode string that is already ...
🌐
HCL GUVI
studytonight.com › python-howtos › how-to-convert-a-string-to-utf8-in-python
HCL GUVI | Learn to code in your native language
HCL GUVI's Data Science Program was just fantastic. It covered statistics, machine learning, data visualization, and Python within its curriculum.