在python中,u和r前缀如何处理字符串?

在python中,u和r前缀如何处理字符串?,python,string,python-2.7,rawstring,unicode-literals,Python,String,Python 2.7,Rawstring,Unicode Literals,我知道我们可以在字符串之前使用r(原始字符串)和u(unicode)标志来获得我们实际需要的内容。但是,我想知道这些如何与字符串一起工作。我在空闲时试过这个: a = r"This is raw string and \n will come as is" print a # "This is raw string and \n will come as is" help(r) # ..... Will get NameError help(r"") # Prints empty Python

我知道我们可以在字符串之前使用r(原始字符串)和u(unicode)标志来获得我们实际需要的内容。但是,我想知道这些如何与字符串一起工作。我在空闲时试过这个:

a = r"This is raw string and \n will come as is"
print a
# "This is raw string and \n will come as is"
help(r)
# ..... Will get NameError
help(r"")
# Prints empty

Python如何知道它应该将字符串前面的
r
u
视为标志?或者作为特定的字符串文本?如果我想了解更多关于字符串文字及其限制的信息,我该如何了解它们?

根据python语法中的定义,
u
r
前缀是字符串文字的一部分。当python解释器解析文本命令以了解该命令的作用时,它将
r“foo”
作为一个字符串文本读取,值为
“foo”
。另一方面,它将
b“foo”
读取为具有等效值的单字节文本

有关更多信息,请参阅python文档中的。此外,python还有一个
ast
模块,允许您探索python解析命令的方式