【问题标题】:How do I use the ascii function in Python 3?如何在 Python 3 中使用 ascii 函数?
【发布时间】:2013-07-02 06:19:32
【问题描述】:

我正在尝试学习如何在 Python 3 中使用 ascii 函数。

文档内容

ascii(对象)

作为 repr(),返回一个包含可打印表示的字符串 对象,但转义返回的字符串中的非 ASCII 字符 repr() 使用 \x、\u 或 \U 转义。这会生成一个类似于 在 Python 2 中由 repr() 返回。

但是,当我尝试使用 print ascii("c") 时,我收到错误:

print ascii("c")
          ^
SyntaxError: invalid syntax

如何使用这个功能?

【问题讨论】:

  • print 是 Python 3 中的一个函数。

标签: python python-3.x ascii


【解决方案1】:

print 是 python3 中的一个函数,您收到该错误是因为您试图将其用作语句。

print (ascii("c"))

演示:

>>> print (ascii("c"))
'c'
>>> print ascii("c")
  File "<ipython-input-3-c44db7d0eada>", line 1
    print ascii("c")
              ^
SyntaxError: invalid syntax

【讨论】:

  • 这很尴尬。谢谢!
猜你喜欢
  • 1970-01-01
  • 2017-05-21
  • 1970-01-01
  • 2011-02-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-06
  • 2022-10-12
  • 2020-03-29
相关资源
最近更新 更多