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 - Both methods produce a bytes object with the UTF-8 representation of the original string. The str.encode method serves as an alternative syntax for achieving the same result ... original_string = &quot;Hello, World!&quot; utf8_string_encoded = original_string.encode('utf-8') utf8_string_str_encode = str.encode(original_string, 'utf-8') print(&quot;Original String:&quot;, original_string) print(&quot;UTF-8 String (Using encode method):&quot;, utf8_string_encoded) print(&quot;UTF-8 String (Using str.encode method):&quot;, utf8_string_str_encode)
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-encode-decode
Python String Encode and Decode: Complete Guide | DigitalOcean
August 3, 2022 - Some other possible values are ‘ignore’, ‘replace’ and ‘xmlcharrefreplace’. Let’s look at a simple example of python string encode() decode() functions. str_original = 'Hello' bytes_encoded = str_original.encode(encoding='utf-8') print(type(bytes_encoded)) str_decoded = bytes_encoded.decode() print(type(str_decoded)) print('Encoded bytes =', bytes_encoded) print('Decoded String =', str_decoded) print('str_original equals str_decoded =', str_original == str_decoded)
🌐
Medium
lynn-kwong.medium.com › understand-the-encoding-decoding-of-python-strings-unicode-utf-8-f6f97a909ee0
Understand the encoding/decoding of Python strings (Unicode/UTF-8) | by Lynn G. Kwong | Medium
August 25, 2023 - In Python 2, a string is by default a binary string and you need to use u'' to mark a string as a Unicode string. However, in Python 3, a string by default is a Unicode string, and you need to use b'' to explicitly mark a string as a binary string.
🌐
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 - However, when it comes to storing ... format, such as UTF-8. Python provides a built-in method called encode() that allows you to encode a string into a specified encoding format, including UTF-8....
🌐
Programiz
programiz.com › python-programming › methods › string › encode
Python String encode()
# 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.
🌐
Python documentation
docs.python.org › 3 › howto › unicode.html
Unicode HOWTO — Python 3.14.5 documentation
February 23, 2026 - 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). The following examples show the differences: >>> b'\x80abc'.decode("utf-8", "strict") Traceback (most recent call last): ...
🌐
W3Schools
w3schools.com › python › ref_string_encode.asp
Python String encode() Method
Python Examples Python Compiler ... The encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used....
Find elsewhere
🌐
MojoAuth
mojoauth.com › character-encoding-decoding › utf-8-encoding--python
UTF-8 Encoding : Python | Encoding Solutions Across Programming Languages
Encoding text in UTF-8 in Python is straightforward, thanks to its built-in support for Unicode. The encode() method is used to convert a string (which is a Unicode object in Python 3) into a bytes object in UTF-8 format.
🌐
Codecademy
codecademy.com › docs › python › strings › .encode()
Python | Strings | .encode() | Codecademy
October 23, 2023 - The .encode() method takes a given string and returns an encoded version of that string. If no encoding specifications are given, UTF-8 is used by default. ... Looking for an introduction to the theory behind programming?
🌐
LabEx
labex.io › tutorials › python-how-to-use-python-utf8-encoding-451217
How to use Python UTF8 encoding | LabEx
LabEx recommends understanding UTF-8 as a fundamental skill for modern Python programming. Encoding and decoding are fundamental processes for converting text between different representations in Python. ## String to bytes encoding text = "Hello, 世界!" encoded_text = text.encode('utf-8') print(encoded_text) ## Converts string to UTF-8 bytes ## Bytes to string decoding decoded_text = encoded_text.decode('utf-8') print(decoded_text) ## Converts bytes back to string
🌐
Vultr Docs
docs.vultr.com › python › standard-library › str › encode
Python str encode() - Encode String | Vultr Docs
December 25, 2024 - Start with a basic string in Python. Use the encode() function to convert it into UTF-8 format.
🌐
Real Python
realpython.com › python-encodings-guide
Unicode & Character Encodings in Python: A Painless Guide – Real Python
May 29, 2024 - Note: If you type help(str.encode), you’ll probably see a default of encoding='utf-8'. Be careful about excluding this and just using "résumé".encode(), because the default may be different in Windows prior to Python 3.6.
🌐
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.
🌐
SSOJet
ssojet.com › character-encoding-decoding › utf-8-in-python
UTF-8 in Python | Encoding Standards for Programming Languages
For instance, my_string = "你好, world!" becomes b'\xe4\xbd\xa0\xe5\xa5\xbd, world!' when encoded to UTF-8. Decoding this bytes object with utf-8 will perfectly restore the original string. A common pitfall is attempting to write a Python string directly to a file opened in binary mode ('wb') without first encoding it.
🌐
Honeybadger
honeybadger.io › blog › python-character-encoding
Python developer's guide to character encoding - Honeybadger Developer Blog
March 6, 2023 - Python uses UTF-8 by default, which means it does not need to be specified in every Python file. To encode a string into bytes, add the encode method, which will return the binary representation of the string.
🌐
Evanjones
evanjones.ca › python-utf8.html
How to Use UTF-8 with Python (evanjones.ca)
October 1, 2005 - You can do this in one of two ways. First, you can place a UTF-8 byte-order marker at the beginning of your file, if your editor supports it. Secondly, you can place the following special comment in the first or second lines of your script: ... Any ASCII-compatible encoding is ...
🌐
Python Guides
pythonguides.com › convert-string-to-utf-8-in-python
How To Convert String To UTF-8 In Python
May 16, 2025 - In Python, strings are stored as Unicode by default. Sometimes, you may need to convert them into UTF-8 bytes. The .encode('utf-8') method takes your string and converts it into a series of bytes using the UTF-8 encoding format.
🌐
Tutorialspoint
tutorialspoint.com › python › string_encode.htm
Python String encode() Method
Welcome to Tutorialspoint." str_encoded= str.encode('utf_16','strict') print("The encoded string is: ", str_encoded) On executing the above program, the following output is generated - The encoded string is: b'\xff\xfeH\x00e\x00l\x00l\x00o\x00!\x00 \x00W\x00e\x00l\x00c\x00o\x00m\x00e\x00 \x00t\x00o\x00 \x00T\x00u\ x00t\x00o\x00r\x00i\x00a\x00l\x00s\x00p\x00o\x00i\x00n\x00t\x00.\x00' The python string encode() method that takes 'euc_kr' as its encoding has a variable length encoding done on korean characters.
🌐
Studytonight
studytonight.com › python-howtos › how-to-convert-a-string-to-utf8-in-python
How to Convert a String to UTF-8 in Python? - Studytonight
February 23, 2021 - In this article, we learned to convert a plain string to utf-8 format using encode() method.