首页 > 文章列表 > python中静态字符串的编码

python中静态字符串的编码

Python 静态字符串
481 2022-08-07

1、静态字符串的编码方法取决于报头中的编码设置。在下面的代码中,变量名用UTF-8编码。

#!/usr/bin/env python
# coding: utf-8
name = "后端技术小屋"

2、如果没有设置编码,运行py文件时会报告语法错误,因为Python Interpreter无法识别文件中的中文字符。

SyntaxError: Non-ASCII character '\xe5' in file 1.py on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

拓展

查看py源文件编码

vim py文件输入

:set fileencoding

输出

fileencoding=utf-8