directory
- A preface.
- The Python STR/bytes/Unicode distinction
- 1. In python2. x, STR/bytes/UN…
- 2.Python3.x STR/bytes/UN…
- Python string and bytes are converted
- 1. String is encoded into bytes
- 2. Bytes are decoded and converted to a string
- Four. Guess you like it
Recommended path for learning Python: Python Learning Directory >> Python Basics
A preface.
Bytearray/bytes/String distinguishes between bytes and characters. For details, see bytearray/bytes/String.
- The STR character is for people to see, for example, the content of the text saved for manipulation;
- Bytes are shown to a computer, such as binary data, for transmission or storage.
The Python STR/bytes/Unicode distinction
1. The STR/bytes/Unicode difference in python2. x
In python2. x, STR and bytes are equivalent; Note that bytes and Unicode are equivalent, as shown in the figure below
#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python STR/bytes/Unicode. Py @time :2021/05/09 07:37 @Motto: A thousand miles without a small stream without a river or sea. The wonderful program life needs to accumulate unremittingly! """ s1 = u"Hello, World!" s2 = "Hello, World!" Print (type (s1)) print (type (s2)) ' ' 'output: < type' unicode > < type > 'STR' ' ' 'Copy the code
2. The STR/bytes/Unicode difference in python3. x
STR is equivalent to Unicode in python3. x; It is worth noting that bytes and Unicode are not equivalent, as shown below
#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python STR/bytes/Unicode. Py @time :2021/05/09 07:37 @Motto: A thousand miles without a small stream without a river or sea. The wonderful program life needs to accumulate unremittingly! """ s1 = u"Hello, World!" s2 = "Hello, World!" Print (type (s1)) print (type (s2)) ' ' 'output: < class' STR '> < class' STR '>' ' 'Copy the code
Python string and bytes are converted
1. String is encoded into bytes
#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python STR/bytes/Unicode. Py @time :2021/05/09 07:37 @Motto: A thousand miles without a small stream without a river or sea. The wonderful program life needs to accumulate unremittingly! """ s = "www.codersrc.com" # convert string to byte object B2 = bytes(s,encoding='utf8') # must specify encoding format # print(b2) # Method one: string encode will obtain a bytes object B4 = s.encode() print(b3) print(type(b3)) print(b4) print(type(b4))" B 'www.codersrc.com' <class 'bytes'> b'www.codersrc.com' <class 'bytes'>"Copy the code
2. Bytes are decoded and converted to a string
#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python STR/bytes/Unicode. Py @time :2021/05/09 07:37 @Motto: A thousand miles without a small stream without a river or sea. The wonderful program life needs to accumulate unremittingly! "" # b2 # if the object contains Chinese, the encoding must be specified, otherwise TypeError will be reported: String argument without an encoding b2 = bytes(" Python ", Encoding ='utf8') # method 2: Decode will get a string s2 = bytes.decode(b2) Print (s2) print(s3)"Copy the code
Four.Guess you like
- Python conditional derivations
- Python list derivations
- Python dictionary derivations
- Python variable argument *argc/**kargcs
- Python anonymous function lambda
- Python return logic determines expressions
- Python is differs from ==
- Python mutable and immutable data types
- Shallow and deep copies of Python
- Python exception Handling
- Python thread creation and parameter passing
- Python thread mutex Lock
- Python thread time Event
- The Python thread Condition variable Condition
- Python thread Timer Timer
- Python thread Semaphore
- Python thread Barrier object Barrier
- Python thread Queue Queue – FIFO
- Python thread queue LifoQueue – LIFO
- Python thread PriorityQueue PriorityQueue
- Python thread Pool ThreadPoolExecutor
- Python thread Pool ThreadPoolExecutor
- The Python Process module
- The Python Process Process is different from threading
- Python interprocess communication Queue/Pipe
- Python process Pool multiprocessing.pool
- Python GIL lock
Python STR/bytes/Unicode
This article is published by the blog – Ape Say Programming Ape Say programming!