Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python运算符和格式_Python - Fatal编程技术网

Python运算符和格式

Python运算符和格式,python,Python,我想知道为什么这个代码中需要这么多 print ("\t/\\\n //\\\ \n ///\\\\\\ ") 正如您所看到的,输出应该看起来像一个金字塔。为什么每个/都需要这么多\个?金字塔的底部只输出3,但必须输入6才能得到3?许多编程语言,包括python,都有。这些字符用于字符串中的特殊字符,如换行符[\n]、回车符[\r]等 斜杠本身用于部分标识转义字符,因此必须对其本身进行转义才能在字符串中定期使用。因此,每个斜杠必须使用两个。这可以追溯到标准,该标准定义了计

我想知道为什么这个代码中需要这么多

print ("\t/\\\n       //\\\ \n      ///\\\\\\ ")

正如您所看到的,输出应该看起来像一个金字塔。为什么每个/都需要这么多\个?金字塔的底部只输出3,但必须输入6才能得到3?

许多编程语言,包括python,都有。这些字符用于字符串中的特殊字符,如换行符[\n]、回车符[\r]等

斜杠本身用于部分标识转义字符,因此必须对其本身进行转义才能在字符串中定期使用。因此,每个斜杠必须使用两个。这可以追溯到标准,该标准定义了计算机使用的许多原始字符


因此,您的\t/\\n/\\\n//\\\\字符串在使用时会对每个字符进行转义。

\用于转义字符。例如,如果要在两个双引号内打印一个双引号,则必须对其进行转义,因为它们是相同的,并且会出现错误

print(" " ")
>> Syntax Error

print(" \" ")
>> "
现在,如果您尝试打印其中的2个,它将只打印1个,因为它正在转义下一个

print(" \\ ")
>> \

# 3rd one doesn't need another one because the next character is a space.
print(" \\\ ")
>> \\


# This one needs another one because there is no space. The next character is the double quote, so if there are only 3, the 3rd one will try to escape it and cause an error.
print("\\\\")
>> \\

print("\\\")
>> Syntax Error

希望这能有所帮助