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)
🌐
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....
🌐
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.
🌐
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....
🌐
Python documentation
docs.python.org › 3 › howto › unicode.html
Unicode HOWTO — Python 3.14.7 documentation
A string of ASCII text is also valid UTF-8 text. UTF-8 is fairly compact; the majority of commonly used characters can be represented with one or two bytes. If bytes are corrupted or lost, it’s possible to determine the start of the next UTF-8-encoded code point and resynchronize.
🌐
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.
🌐
Programiz
programiz.com › python-programming › methods › string › encode
Python String encode()
Online Python Online JavaScript Online SQL Online Java Online HTML Online C Online C++ Online C# Online PHP Online Swift Online Kotlin Online TypeScript Online Go Online Rust Online Scala Online Dart Online R Online Ruby ... The encode() method returns an encoded version of the given string. ... ...
Find elsewhere
🌐
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.
🌐
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.
🌐
Vultr Docs
docs.vultr.com › python › standard library › str › encode()
Python str encode() - Encode String
December 25, 2024 - original_string = "Python programming is fun!" encoded_string = original_string.encode('utf-8') print(encoded_string)
🌐
Real Python
realpython.com › python-encodings-guide
Unicode & Character Encodings in Python: A Painless Guide – Real Python
May 20, 2019 - 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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-strings-encode-method
Python - Strings encode() method - GeeksforGeeks
July 11, 2025 - The encode("utf-8") method converts the string into a bytes object.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-encode-decode
Python String Encode and Decode: Complete Guide | DigitalOcean
Master Python string encode() and decode() methods. Learn UTF-8, ASCII, Unicode handling, error handling modes, and practical encoding/decoding examples.
🌐
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.
🌐
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
🌐
Real Python
realpython.com › lessons › encoding-utf8
Encoding UTF-8 (Video) – Real Python
01:20 Notice that these are the code points, not how UTF-8 stores them. 01:27 You can use the "\u" to get those letters back out. So, I can replace place with 'caf\u00E9' and get back the exact same string. ... 01:55 Now, when you encode this, notice that the code point 'E9' turns into 0xc30xa9 (c3 a9)—2 bytes of hex.
Published: June 30, 2020
🌐
SSOJet
ssojet.com › character-encoding-decoding › utf-8-in-python
UTF-8 in Python | Encoding Standards for Programming Languages
To avoid errors, consistently encode ... byte data. When working with text files in Python, explicitly specifying the encoding='utf-8' parameter in the open() function is crucial for accurate character handling....
🌐
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.