EP 3 Know the Differences Between bytes, str, and unicode

  • In Python 3, bytes contains sequences of 8 bit values, str contains sequences of Unicode characters. bytes and str instances cannot be used together with operators such as < or +

Effective Python

Helper function to ensure str or bytes

def to_str(bytes_or_str):
    if instance(bytes_or_str, bytes):
        value = bytes_ot_str.decode('utf-8')
    else:
        value = bytes_or_str
    return value
    
    
def to_bytes(bytes_or_str):
    if isinstance(bytes_or_str, bytes):
        value = bytes_or_str).encode('utf-8')
    else:
        value = bytes_or_str
    return value

Recommended Posts

EP 3 Know the Differences Between bytes, str, and unicode
str and unicode
Summary of the differences between PHP and Python
The rough difference between Unicode and UTF-8 (and their friends)
A rough summary of the differences between Windows and Linux
I tried to enumerate the differences between java and python
Differences between yum commands and APT commands
Differences between symbolic links and hard links
Differences between Python, stftime and strptime
Python2 str / unicode and encode / decode
Differences in authenticity between Python and JavaScript
Differences between Ruby and Python in scope
Differences in syntax between Python and Java
Easily organize the differences between Apache Tomcat
The subtle relationship between Gentoo and pip
About the relationship between Git and GitHub
Matplotlib Basics / Differences between fig and axes
Differences between Numpy 1D array [x] and 2D array [x, 1]
Differences in multithreading between Python and Jython
Differences between Django's request.POST ['hoge'] and request.POST.get ('hoge')
Differences between Ruby and Python (basic syntax)
What is the difference between `pip` and` conda`?
Differences between queryStringParameters and multiValueQueryStringParameters in AWS Lambda
The answer of "1/2" is different between python2 and 3
About the difference between "==" and "is" in python
Differences and commonalities between dict, list, and tuple types
Differences between glibc, musl libc and go resolvers
About the difference between PostgreSQL su and sudo
What is the difference between Unix and Linux?