How to convert a string to utf-8 in Python
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Python 3, text and encoded bytes are different things. That matters when people say they want to "convert a string to UTF-8," because a Python str already represents Unicode text, while UTF-8 is a byte encoding used when you store or transmit that text.
Understand str versus bytes
The most important concept is that UTF-8 is not another flavor of Python string. A Python str is decoded text. To get UTF-8 data, you encode the text into a bytes object.
Typical output looks like this:
If your goal is to send text over a socket, write it to a file, or store it in a system that expects bytes, this is the correct conversion.
Decode bytes back into text when needed
If you already have UTF-8 bytes and want a Python string, you do the opposite operation with decode.
This prints the decoded Unicode text. Keeping the distinction clear prevents a lot of encoding confusion:
- '
str.encode(...)converts text to bytes' - '
bytes.decode(...)converts bytes to text'
Handle file input and output explicitly
A common place to work with UTF-8 is file I/O. The cleanest approach is usually to let Python handle the encoding at the file boundary.
This is often better than manually encoding and decoding unless you truly need raw bytes. It also makes the code's intent obvious to the next person reading it.
Encoding is different from Unicode normalization
Sometimes text "looks wrong" even after correct UTF-8 encoding because the issue is not encoding at all. Some characters can be represented in more than one Unicode form, so normalization may matter too.
This is not a UTF-8 conversion step by itself, but it is an important distinction when apparently equivalent strings compare differently.
Converting from another encoding
Sometimes the real problem is not "convert a string to UTF-8" but "I received bytes in the wrong encoding and need UTF-8 output." In that case, decode using the original encoding first, then encode as UTF-8.
That two-step process matters because Python cannot correctly turn arbitrary bytes into UTF-8 unless it knows what those bytes currently mean.
Use error handling deliberately
Real input is messy. You may encounter bytes that cannot be decoded with the encoding you expected. Python lets you choose how strict to be.
Common errors modes include:
- '
strictto raise an exception' - '
replaceto substitute invalid data' - '
ignoreto skip bad bytes'
For debugging and data quality, strict is often best. For user-facing pipelines where partial recovery is acceptable, replace can be practical.
Common Pitfalls
The biggest pitfall is trying to call encode on bytes or decode on text without checking the current type first. If you double-encode or double-decode data, the result is either an exception or corrupted text.
Another common problem is assuming every text file is UTF-8. Many systems still emit Latin-1, Windows-1252, Shift-JIS, or other encodings. When the source bytes are not really UTF-8, calling .decode("utf-8") will fail or produce replacement characters.
It is also easy to confuse display issues with storage issues. A terminal or editor may render characters incorrectly even when the data itself is valid UTF-8. In that case, the problem is the display environment, not the Python code.
Finally, do not "convert a string to UTF-8" unless you actually need bytes. Inside most Python 3 application code, keeping values as str for as long as possible is simpler and safer.
Summary
- In Python 3,
stris Unicode text and UTF-8 is a byte encoding. - Use
text.encode("utf-8")to get UTF-8 bytes from a string. - Use
data.decode("utf-8")to turn UTF-8 bytes into a string. - Specify
encoding="utf-8"at file boundaries to keep I/O explicit. - Decode from the original encoding first when incoming bytes are not already UTF-8.
Related reading
- How to convert a UTC datetime to a local datetime using only standard library?
- How to convert an array of strings to an array of floats in numpy?
- How to convert 'binary string' to normal string in Python3?
- How to convert comma-delimited string to list in Python?
- How to convert from tensor to float
- How to convert hexadecimal string to bytes in Python?
- How to convert index of a pandas dataframe into a column
- How to convert index of a pandas dataframe into a column
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.