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.
Discussions

Convert Text file from UTF-16 LE to UTF-8
If you're trying to open a file that is utf16le why aren't you trying that encoding? text_file = open('test.txt', 'r', encoding='utf-16-le') More on reddit.com
🌐 r/learnpython
2
1
December 3, 2020
How to convert HTTP query to UTF-8?
I’m able to get any HTTP GET/POST query, However, only in ASCII mode. When I entered the text áaa@bbb.cc’ in an input field, I got at my server the text ‘aaa@bbb.cc’. Apparently I have to convert the text ‘@’ into ‘… More on discuss.python.org
🌐 discuss.python.org
0
0
March 20, 2023
encoding - How to convert a file to utf-8 in Python? - Stack Overflow
Yes, this answer was posted 8 years ago, so it's a piece of old Python 2 code. 2016-10-26T15:42:08.13Z+00:00 ... I try this code, I tun it, but it doesn't convert ANSI text files to UTF-8... More on stackoverflow.com
🌐 stackoverflow.com
How to convert cmd output to UTF-8
This depends on what you do after receiving. You are getting a byte string, how do you convert it to str for printing? You need to decode it from the remote iso encoding to bytes, probably before sending, then encode it to whatever your terminal displays, probably utf-8. More on reddit.com
🌐 r/learnpython
11
14
October 2, 2021
🌐
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 - The encode() method provides a straightforward way to achieve this, allowing you to specify the desired character encoding. Understanding and using character encoding correctly ensures that your Python applications can handle text data accurately ...
🌐
Python Guides
pythonguides.com › convert-string-to-utf-8-in-python
How To Convert String To UTF-8 In Python
May 16, 2025 - The .encode('utf-8') method takes your string and converts it into a series of bytes using the UTF-8 encoding format. text = "Hello, world!" utf8_encoded = text.encode('utf-8') print(utf8_encoded) print(type(utf8_encoded))
🌐
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.
🌐
Python documentation
docs.python.org › 3 › howto › unicode.html
Unicode HOWTO — Python 3.14.6 documentation
This is especially true if the ... text in the encoded bytestream. The StreamRecoder class can transparently convert between encodings, taking a stream that returns data in encoding #1 and behaving like a stream returning data in encoding #2. For example, if you have an input file f that’s in Latin-1, you can wrap it with a StreamRecoder to return bytes encoded in UTF-8...
🌐
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.
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 - We discussed the basics of encoding in Python, highlighting the difference between Python 2 and Python 3. The encode() function is used to encode string to UTF-8 in Python.
🌐
Teleport
goteleport.com › home › resources › tools › utf-8 encoder | instantly transform text to utf-8 hex
UTF-8 Encoder | Instantly Transform Text to UTF-8 Hex | Teleport
In fact, since Python 3.0, UTF-8 has been the default source encoding. This means that you can use Unicode characters directly in your Python code without any special notation. Here's an example of encoding and decoding a string in Python: text = "Café" encoded_text = text.encode("utf-8") print(encoded_text) # Output: b'Caf\xc3\xa9' decoded_text = encoded_text.decode("utf-8") print(decoded_text) # Output: Café
🌐
Reddit
reddit.com › r/learnpython › convert text file from utf-16 le to utf-8
r/learnpython on Reddit: Convert Text file from UTF-16 LE to UTF-8
December 3, 2020 -

Hey guys I'm trying to convert a text from from utf16le to utf8 and I seen many code examples and have tried them but I keep getting errors. Here's my code:

text_file = open('test.txt', 'r', encoding='utf8')
lines = text_file.read()

text_file.close()
print(lines)

Here's the error I get:

Traceback (most recent call last):
  File "c:\Users\Yaasir.Mahamadou\Desktop\trufflehog-script\trufflehog_parser.py", line 8, in <module>
    lines = text_file.read()
  File "C:\Users\Yaasir.Mahamadou\AppData\Local\Programs\Python\Python38\lib\codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

Any helps appreciated, thanks!

🌐
Gree2
gree2.github.io › python › 2015 › 04 › 22 › python-read-file-encoding-and-convert-to-utf-8
python read file encoding and convert to utf-8
$ python setup.py install · reference determine the encoding of text file · import magic blob = open('file.csv').read() m = magic.Magic(mime_encoding=True) encoding = m.from_buffer(blob) print(encoding) reference how to convert a file to utf-8 in python · source = open('file.csv') target = open('file_utf8.csv', "w") target.write(unicode(source.read(), encoding).encode("utf-8")) but after convert this file cannot cat ·
🌐
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 - The built-in function encode() is applied to a Unicode string and produces a string of bytes in the output, used in two arguments: the input string encoding scheme and an error handler. Any encoding can be used in the encoding scheme: ASCII, UTF-8 (used by default), UTF-16, latin-1, etc.
🌐
Python.org
discuss.python.org › python help
How to convert HTTP query to UTF-8? - Python Help - Discussions on Python.org
March 20, 2023 - I’m able to get any HTTP GET/POST query, However, only in ASCII mode. When I entered the text áaa@bbb.cc’ in an input field, I got at my server the text ‘aaa@bbb.cc’. Apparently I have to convert the text ‘@’ into ‘@’ via chr(int(‘40’, 16)). But what about the text ‘украї́нська’, because I got at my server the text ‘Україна’? How can I convert it?
🌐
CodeShack
codeshack.io › home › tools › utf-8 encoder
UTF-8 Encoder - Convert Text to UTF-8 Bytes (Hex, Binary, Decimal)
Free UTF-8 encoder. Convert any text into its UTF-8 byte sequence in hexadecimal, binary or decimal, with custom separators, prefixes and hex case. Fast, private and 100% in your browser.
🌐
Teleport
goteleport.com › home › resources › tools › utf-8 decoder | instantly transform utf-8 hex to readable text
UTF-8 Decoder | Instantly Transform UTF-8 Hex to Readable Text | Teleport
Invalid byte values: Bytes with values greater than 0xF4 or in the range of 0xC0 to 0xC1 are not allowed in UTF-8. To handle these cases gracefully, you can use lenient decoding modes or replace invalid characters with a placeholder, such as the Unicode Replacement Character (U+FFFD) '�'. For example, in Python: decoded_text = bytes.decode('utf-8', errors='replace')
🌐
Charset
charset.org › charset tools › programming: converting latin to unicode (utf-8)
Programming: Converting Latin to Unicode (UTF-8) - Charset Tools
For case conversions use multibyte-aware functions: mb_strtolower(), mb_strtoupper(), mb_convert_case(). Avoid saving PHP source files with a UTF-8 BOM — it can emit invisible bytes before your output. ... # if you have raw bytes in ISO-8859-1 (Latin1) latin_bytes = b"Names with international characters like Andr\xe9e" # decode to a Python 3 str (Unicode) text = latin_bytes.decode('iso-8859-1') # get UTF-8 encoded bytes utf8_bytes = text.encode('utf-8') # Python 3 strings are Unicode; 'text' already holds the correct Unicode string print(text)
🌐
Reddit
reddit.com › r/learnpython › converting text encoding to utf8.
r/learnpython on Reddit: Converting text encoding to utf8.
April 4, 2024 - My guess is that my data contains names which are in some other format, and python is attempting to resolve the formatting by inserting escape characters. Any help is appreciated. EDIT: I figured it out. The following code resolved the issue. full_name = bytes(full_name_raw, 'utf-8').decode('unicode_escape').encode('latin1').decode('utf8')
🌐
AskPython
askpython.com › python › examples › binary-to-utf8-conversion
How to Convert Binary Data to UTF-8 in Python - AskPython
April 10, 2025 - data = b"Some binary data" text = data.decode("utf-8") Note that UTF-8 is the default encoding for decode(), so you can omit it: ... This handles the majority of the UTF-8 decoding use cases. Also read: Encoding an Image File With BASE64 in Python
🌐
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.