Home > Backend Development > Python Tutorial > What encoding is used in python3

What encoding is used in python3

(*-*)浩
Release: 2019-07-20 11:25:42
Original
3656 people have browsed it

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 -*-
Copy after login

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")
Copy after login

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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template