directory

  • Python bytes are different from strings
  • Python string to bytes
  • Python bytes to string
  • Four. Guess you like it

Recommended courses for learning Basic Python: Python Learning Directory >> Getting Started with Python

Python bytes are different from strings

  • **Python bytes are also called sequences of bytes, not characters. The value ranges from 0 <= bytes <= 255. The output is preceded by the character B. String ** is a string type in Python;

  • Bytes are for computers. Strings are for people.

  • 3. String is encoded and converted into binary objects for computer recognition; Bytes are decoded to string by decode. Let’s see, but note that the encoding rules for unencoding are ranges. \xc8 is not a range recognized by UTF8;

    ! usr/bin/env python

    – coding:utf-8 _

    “” @author: Ape says programming @blog (personal Blog address): www.codersrc.com @file :Python bytes and string conversion. Py @time :2021/04/29 08:00 @Motto: A thousand miles without a small step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance!

    “” “

    if name == “main”: Print (b) print(type(b)) print(s) print(type(s))

    Output result:

    b’www.codersrc.com’ <class ‘bytes’> www.codersrc.com <class ‘str’> ”’

Python string to bytes

The string is encoded into bytes, the sample code is as follows:

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python bytes and string conversion py @time :2021/04/29 08:00 @Motto: A thousand miles without a small stream without a river. The wonderful program life needs to accumulate unremittingly! """ if __name__ == "__main__": S = "www.codersrc.com" # convert string to byte object b2 = bytes(s, B3 = str.encode(s) 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

Python bytes to string

Bytes are converted to string by decode decode. The code for this example is as follows:

if __name__ == "__main__": "www.codersrc.com" print(b) b = bytes(" python", Encoding ='utf8') print(b) s2 = bytes.decode(b) s3 = b.decode() print(s2) print(s3)" B 'www.codersrc.com' b'\xe7\x8c\ XBF \xe8\xaf\xb4pythonCopy the code

Four.Guess you like

  1. The Python for loop
  2. The Python string
  3. The Python list
  4. The Python tuple tuple
  5. Python dictionary dict
  6. Python conditional derivations
  7. Python list derivations
  8. Python dictionary derivations
  9. Python function declarations and calls
  10. Python variable argument *argc/**kargcs
  11. Python anonymous function lambda
  12. Python return logic determines expressions
  13. Python string/list/tuple/dictionary conversions
  14. Python local and global variables
  15. The Python type function is different from the isinstance function
  16. Python is differs from ==
  17. Python mutable and immutable data types
  18. Shallow and deep copies of Python

Python bytes and string are converted to each other

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