V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
lvfujun
V2EX  ›  问与答

python 声明字符串前加 r 代表的是什么?

  •  
  •   lvfujun · 2015-06-18 12:13:03 +08:00 · 3426 次点击
    这是一个创建于 3242 天前的主题,其中的信息可能已经有所发展或是发生改变。
    ```python
    s3 = r'Hello, "Bart"'
    print(s3)
    #结果是:'Hello, "Bart"'
    请问 r'Hello, "Bart"' 这段字符串引号前面的r有什么含义.
    ```
    5 条回复    2015-06-18 13:12:17 +08:00
    fangjinmin
        2
    fangjinmin  
       2015-06-18 12:17:01 +08:00   ❤️ 1
    r代表raw就是原始的文字列,不转义。
    popu111
        3
    popu111  
       2015-06-18 12:17:40 +08:00   ❤️ 1
    原始字符串
    lvfujun
        4
    lvfujun  
    OP
       2015-06-18 12:45:02 +08:00
    MaiCong
        5
    MaiCong  
       2015-06-18 13:12:17 +08:00 via iPhone
    http://sebug.net/paper/python/ch04s03.html

    自然字符串

    如果你想要指示某些不需要如转义符那样的特别处理的字符串,那么你需要指定一个自然字符串。自然字符串通过给字符串加上前缀r或R来指定。例如r"Newlines are indicated by \n"。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2129 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 15:55 · PVG 23:55 · LAX 08:55 · JFK 11:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.