1

I'm trying to use triple quotes to designate a large comment, but I'm getting a new error that says

SyntaxError: invalid string (possibly contains a unicode character) on line 2

Here's the code.

"""

Write a program that asks the user how many names they have. 
(If they have a first name, two middle names, and a last 
name, for example, they would type 4.) Then, using a for 
loop, ask the user for each of their names. Finally, print 
their full name.

"""

The triple quotes that I'm having a problem with are the ones at the top.

With that information out of the way, my question is "Are triple quotation marks unicode characters according to python?"

If this is a repeat post, feel free to move it.

4

1 回答 1

0

在您的示例中,提供的字符都是有效的 ASCII 字符Basic Latin Unicode 块恰好具有与 ASCII 完全相同的字符集)。因此,这不会在标准 Python 解释器(如CPythonPyPy )中引起任何问题(无论是 Python 2 还是 Python 3)。

根据Python,三引号是Unicode字符吗?

从技术上讲,所有 ASCII 字符都包含在 Unicode 中,但我认为您真正要问的是“三引号是非 ASCII 字符吗?”,答案是“否”。它们只是连续三个引号( ")。

>>> for c in '"""':
...     print('Character:', repr(c))
...
Character: '"'
Character: '"'
Character: '"'

您还可以使用以下方法在 Python 3.7+ 中轻松检查str.isascii

>>> '"""'.isascii()
True
>>> '""☺'.isascii()
False

过去,要在源文件中使用 Unicode 文字,您需要在源文件的第一行或第二行中包含特殊注释(例如# -*- coding: utf-8 -*-),但从 Python 3开始,源文件默认使用 UTF-8

于 2020-02-19T06:37:47.077 回答