How do I check if a string is unicode or ascii?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Python 3, a str is Unicode text by definition. So the real question is usually not "is this string Unicode or ASCII" but rather "does this Unicode string contain only ASCII characters." Once you make that distinction, the check becomes straightforward.
Understand the Terminology First
ASCII is a small character set covering 128 code points. Unicode is the much larger character model Python 3 str objects use internally.
That means:
- every Python 3
stris Unicode text - some Unicode strings contain only ASCII characters
- ASCII is effectively a subset of Unicode
So a string is not normally one or the other. In Python 3, it is Unicode, and it may or may not be ASCII-only.
The Simple Python 3 Check
Python 3 provides str.isascii().
This returns True only if every character is in the ASCII range.
That is the cleanest answer in modern Python.
Encoding Check With encode
Another way to test ASCII compatibility is to try encoding to ASCII and catch UnicodeEncodeError.
This approach is useful when you are already dealing with encoding boundaries and want behavior that matches actual encoding rules.
Bytes Are Different From Strings
Do not confuse bytes with str. A byte sequence is not automatically decoded text.
If you receive bytes from a file or network socket, the real problem is usually choosing the correct decoding, not checking whether the raw bytes are "Unicode."
In Python, Unicode applies to decoded text objects. Encodings apply when converting between bytes and text.
Legacy Python 2 Context
If you are maintaining old Python 2 code, the terminology gets messier because str and unicode were separate types there. That is one reason this question appears so often in old discussions. In modern Python 3 code, you should simplify the mental model immediately: text is Unicode, and ASCII is just one restricted subset of that text.
Practical Decision Rule
Use isascii() when you want to validate that text contains only simple ASCII characters. Use explicit encoding and decoding when you are crossing I/O boundaries such as files, subprocesses, HTTP, or databases.
This distinction matters because many encoding bugs come from mixing up:
- text content
- byte storage
- character set limits
Once those three ideas are separated, the code usually becomes much clearer.
Common Pitfalls
The most common mistake is asking whether a Python 3 string is Unicode or ASCII as if those were two separate runtime string types. In Python 3, str is Unicode.
Another issue is checking a byte sequence without decoding it first. Bytes are encoded data, not text characters.
Developers also sometimes use isascii() when the real requirement is validation against a business rule, not against the ASCII character set. Those are different checks.
Finally, if your code interacts with external systems, the real source of problems is often inconsistent encoding at input or output time rather than the content of the in-memory string itself.
Summary
- In Python 3,
strobjects are Unicode text. - The useful question is usually whether the text is ASCII-only.
- Use
s.isascii()for the simplest modern check. - Use
encode("ascii")with error handling when you want an encoding-based test. - Keep text-versus-bytes distinctions clear when debugging encoding issues.

