Home  >  Article  >  Backend Development  >  What encoding is used in python3

What encoding is used in python3

(*-*)浩
(*-*)浩Original
2019-07-20 11:25:423617browse

The default encoding in python3 is utf-8. In terms of storage and display, python3 uses text characters and binary data to distinguish, which is more clear and clear.

What encoding is used in python3

By default, Python 3 source files are encoded in UTF-8, and all strings are unicode strings. (Recommended learning: Python video tutorial)

Of course you can also specify different encodings for the source code file:

# -*- coding: cp-1252 -*-

Text characters are represented by the str type, and str can represent Unicode All characters in the character set, and binary data is represented by the bytes type.

Conversion between str and bytes

      # bytes object
      b = b"example"
     
      # str object
      s = "example"
     
      # str to bytes
      bytes(s, encoding = "utf8")
     
      # bytes to str
      str(b, encoding = "utf-8")

Uses utf-8 by default

     # bytes object
      b = b"example"
     
      # str object
      s = "example"
      
      # an alternative method
      # str to bytes
      str.encode(s)
     
      # bytes to str
      bytes.decode(b)

For more Python related technical articles, please visit the Python Tutorial column Get studying!

The above is the detailed content of What encoding is used in python3. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn