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

  1. Python conditional derivations
  2. Python list derivations
  3. Python dictionary derivations
  4. Python variable argument *argc/**kargcs
  5. Python anonymous function lambda
  6. Python return logic determines expressions
  7. Python is differs from ==
  8. Python mutable and immutable data types
  9. Shallow and deep copies of Python
  10. Python exception Handling
  11. Python thread creation and parameter passing
  12. Python thread mutex Lock
  13. Python thread time Event
  14. The Python thread Condition variable Condition
  15. Python thread Timer Timer
  16. Python thread Semaphore
  17. Python thread Barrier object Barrier
  18. Python thread Queue Queue – FIFO
  19. Python thread queue LifoQueue – LIFO
  20. Python thread PriorityQueue PriorityQueue
  21. Python thread Pool ThreadPoolExecutor
  22. Python thread Pool ThreadPoolExecutor
  23. The Python Process module
  24. The Python Process Process is different from threading
  25. Python interprocess communication Queue/Pipe
  26. Python process Pool multiprocessing.pool
  27. Python GIL lock

Python STR/bytes/Unicode

This article is published by the blog – Ape Say Programming Ape Say programming!