【问题标题】:How to declare accented variables in Python如何在 Python 中声明重音变量
【发布时间】:2015-08-07 15:20:15
【问题描述】:

我需要用 Python 为学生编写一些基本脚本,比如这个:

#!/usr/bin/python
# -*- coding: utf-8 -*-

mia_età = 31
print mia_età

但显然我在声明变量时不能使用重音字符。有什么出路吗?

(“mia_età”在意大利语中的意思是“my_age”,我想避免他们在学习 Python 时用他们的第一种语言编写语法错误)

【问题讨论】:

    标签: python character-encoding unicode


    【解决方案1】:

    Python 1 和 2 仅支持 ASCII 字母数字字符和 identifiers 中的 _

    Python 3 支持 identifiers 中的所有 Unicode 字母和某些标记。

    #!/usr/bin/python3
    # -*- coding: utf-8 -*-
    mia_età = 31
    print(mia_età)
    

    根据 NFKC,标识符为 normalized,因此您可以随意写 U+0061 拉丁小写字母 A 后跟 U+0301 COMBINING ACUTE ACCENT,或 U+00E1 拉丁小写字母 A WITH ACUTE。

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 2012-06-15
      • 1970-01-01
      • 2021-04-12
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-15
      相关资源
      最近更新 更多