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.
🌐
Python documentation
docs.python.org › 3 › howto › unicode.html
Unicode HOWTO — Python 3.14.6 documentation
On Unix systems, there will only be a filesystem encoding. if you’ve set the LANG or LC_CTYPE environment variables; if you haven’t, the default encoding is again UTF-8. The sys.getfilesystemencoding() function returns the encoding to use on your current system, in case you want to do the encoding manually, but there’s not much reason to bother. When opening a file for reading or writing, you can usually just provide the Unicode string as the filename, and it will be automatically converted to the right encoding for you:
🌐
MojoAuth
mojoauth.com › character-encoding-decoding › utf-8-encoding--python
UTF-8 Encoding : Python | Encoding Solutions Across Programming Languages
\xe4\xbd\xa0\xe5\xa5\xbd\xef\xbc\x8c\xe4\xb8\x96\xe7\x95\x8c\xef\xbc\x81' # Decoding the bytes back to a string decoded_text = encoded_text.decode('utf-8') # Displaying the decoded string print(decoded_text) # Output: Hello, World! 你好,世界! · In this example, the previously encoded bytes are decoded back to their original string format, demonstrating the seamless transition between encoding and decoding in Python.
🌐
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 - Python, being a versatile and widely used programming language, provides robust support for various character encodings, including UTF-8. In this article, we will explore how to encode a string to UTF-8 in Python. Character encoding is the process of converting characters into a specific format ...
🌐
Python Guides
pythonguides.com › convert-string-to-utf-8-in-python
How To Convert String To UTF-8 In Python
May 16, 2025 - This approach ensures that all text is properly encoded and decoded as UTF-8, preventing those frustrating encoding errors that can plague file I/O operations. Check out How to Insert a Python Variable into a String? If you’re working with legacy code in Python 2, it’s important to note the differences in string handling: # Python 2 (not runnable in Python 3) # In Python 2, you'd use unicode objects unicode_string = u"Hello, 世界!" utf8_string = unicode_string.encode('utf-8') # Python 3 # In Python 3, all strings are Unicode by default normal_string = "Hello, 世界!" utf8_bytes = normal_string.encode('utf-8')
🌐
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 - To encode string to UTF-8 in Python, use the codecs.encode() function. See the code below. ... Python has a standard module called codecs which defines the base class for all the encoders and decoders in Python.
🌐
YouTube
youtube.com › the python oracle
How to convert a string to utf-8 in Python - YouTube
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn--Music by Eric Matyashttps://www.soundimage.orgTrack title: Hypnotic...
Published   March 7, 2023
Views   121
Find elsewhere
🌐
Programiz
programiz.com › python-programming › methods › string › encode
Python String encode()
Using the string encode() method, you can convert unicode strings into any encodings supported by Python. By default, Python uses utf-8 encoding.
🌐
Fedingo
fedingo.com › home
Fedingo - Tech tutorials, How To's & User guides
Sometimes you may need to run a background process in Python. Here are the steps to do this using subprocess module. ... Here are 5 different ways to check if substring is in list of strings in Python.
🌐
W3Schools
w3schools.com › python › ref_string_encode.asp
Python String encode() Method
Python Examples Python Compiler ... Python Interview Q&A Python Bootcamp Python Training ... The encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used....
🌐
SSOJet
ssojet.com › character-encoding-decoding › utf-8-in-python
UTF-8 in Python | Encoding Standards for Programming Languages
UTF-8 is a variable-length encoding, meaning a single character might be represented by one to four bytes. When you encounter raw byte data, perhaps from a network socket or a binary file, you’ll need to decode it into a Python string to work with it as text. For instance, if you have byte_data = b'\xe4\xbd\xa0\xe5\xa5\xbd, \xe4\xb8\x96\xe7\x95\x8c!', you can convert it to a human-readable string using the .decode() method:
🌐
YouTube
m.youtube.com › shorts › CRvaez5frWw
How to convert a string to utf-8 in Python #shorts
Share your videos with friends, family, and the world
Published   June 9, 2024
🌐
Diveintopython3
diveintopython3.net › strings.html
Strings - Dive Into Python 3
In the previous example, the decoding was relatively straightforward — converting a sequence of bytes in the ASCII encoding into a string of characters. But the same process works with any encoding that supports the characters of the string — even legacy (non-Unicode) encodings. >>> a_string = '深入 Python' ① >>> len(a_string) 9 >>> by = a_string.encode('utf-8') ② >>> by b'\xe6\xb7\xb1\xe5\x85\xa5 Python' >>> len(by) 13 >>> by = a_string.encode('gb18030') ③ >>> by b'\xc9\xee\xc8\xeb Python' >>> len(by) 11 >>> by = a_string.encode('big5') ④ >>> by b'\xb2`\xa4J Python' >>> len(by) 11 >>> roundtrip = by.decode('big5') ⑤ >>> roundtrip '深入 Python' >>> a_string == roundtrip True
🌐
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)
🌐
HCL GUVI
studytonight.com › python-howtos › how-to-convert-a-string-to-utf8-in-python
HCL GUVI | Learn to code in your native language
February 23, 2021 - Established in 2014 and acquired by the HCL Group in 2022, HCL GUVI is dedicated to providing effective and high-quality learning and skilling programs that transcend language barriers in technology education. HCL GUVI today is trusted by over 4.8 million users, 20 languages and 2500+ corporate partners.