python str / bytes / unicode explain the difference (49)

table of Contents

I. Introduction

Two .str / bytes / unicode difference

1. str in python2.x version / bytes / unicode difference

2. str in python3.x version / bytes / unicode difference

Three .string and bytes conversion

1.string converted to encode the encoded bytes

2.bytes converted to decode the decoded string

 


I. Introduction

Explaining  str  /  bytes  before / unicode difference we must first understand the difference between bytes and characters, please refer to: ByteArray / bytes / String difference  in a clear explanation of bytes and characters, the most important thing is to understand:

Str character posters are, for example: text saved for operation;

Bytes is byte to the computer to see, for example: binary data, transmitted or stored to the computer;

 

Two .str / bytes / unicode difference

1. str in python2.x version / bytes / unicode difference

In python2.x version of str with the bytes are equivalent; worth noting: bytes with unicode equivalent, see below chart

s1 = u"Hello, World!"
s2 = "Hello, World!"
print(type(s1))
print(type(s2))

Output:

<type 'unicode'>
<type 'str'>

 

2. str in python3.x version / bytes / unicode difference

In python3.x version with unicode str are equivalent; worth noting: bytes with unicode are not equivalent , as detailed in the following figure

 

s1 = u"Hello, World!"
s2 = "Hello, World!"
print(type(s1))
print(type(s2))

Output:

<class 'str'>
<class 'str'>

 

 

Three .string and bytes conversion

1.string converted to encode the encoded bytes

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:何以解忧
@Blog(个人博客地址): shuopython.com
@WeChat Official Account(微信公众号):猿说python
@Github:www.github.com

@File:python_bytes_string_4.py
@Time:2020/3/4 10:25

@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
"""


s = "shuopython.com"
#将字符串转换为字节对象
b2 = bytes(s,encoding='utf8') #必须制定编码格式
# print(b2)

#方法一:字符串encode将获得一个bytes对象
b3 = str.encode(s)
#方法二:字符串encode将获得一个bytes对象
b4 = s.encode()
print(b3)
print(type(b3))
print(b4)
print(type(b4))

Output:

b'shuopython.com'
<class 'bytes'>
b'shuopython.com'
<class 'bytes'>

 

2.bytes converted to decode the decoded string

# 字节对象b2
    # 如果含有中文,必须制定编码格式,否则报错TypeError: string argument without an encoding
    b2 = bytes("猿说python", encoding='utf8')

    # 方法二:bytes对象decode将获得一个字符串
    s2 = bytes.decode(b2)
    # 方法二:bytes对象decode将获得一个字符串
    s3 = b2.decode()
    print(s2)
    print(s3)

Output:

猿说python
猿说python

 

 

you may also like:

1.python bytearray/bytes/string区别

2.python bytes

3.python bytearray

4.python deep copy and shallow copy

5.python local and global variables

 

Reproduced please specify : ape say Python  »  Python Python str / bytes / Unicode explain the difference

 

                                                                      Technical exchanges, business cooperation please contact bloggers

                                                                             Scan code or search: ape say python

No public python tutorial

                                                                                  Ape say python

                                                                          No. sweep the micro-channel public concern

Published 130 original articles · won praise 92 · views 30000 +

Guess you like

Origin blog.csdn.net/ZhaDeNianQu/article/details/104721935