python - 如何在 Python 3 中使用 ascii 函数?

标签 python python-3.x ascii

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

文档内容如下

ascii(object)

As repr(), return a string containing a printable representation of an object, but escape the non-ASCII characters in the string returned by repr() using \x, \u or \U escapes. This generates a string similar to that returned by repr() in Python 2.

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

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

如何使用此功能?

最佳答案

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

关于python - 如何在 Python 3 中使用 ascii 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17418681/

相关文章:

python-3.x - 如何在pd.io.sql.get_schema中指定sql风格?

python - 不使用堆栈的反向队列

python - 从字符串中删除特殊字符,例如笑脸,但保留德语特殊字符

javascript - 使用javascript将带有类名的span添加到字符串的某些字符

python - Django : Making exc_info optional in logging formatter

python - Tkinter Canvas 存储的项目被清除和召回

python - Cron 作业、任务队列或延迟任务?

python - 评估变量包含 f 字符串中的数学表达式

c - 没有 printf 的用户定义函数,返回十六进制值

python - 无法安装 python 模块,如 pip、selenium 等