lvfujun
V2EX  ›  问与答

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

  •  
  •   lvfujun · Jun 18, 2015 · 4124 views
    This topic created in 3983 days ago, the information mentioned may be changed or developed.
    ```python
    s3 = r'Hello, "Bart"'
    print(s3)
    #结果是:'Hello, "Bart"'
    请问 r'Hello, "Bart"' 这段字符串引号前面的r有什么含义.
    ```
    5 replies    2015-06-18 13:12:17 +08:00
    fangjinmin
        2
    fangjinmin  
       Jun 18, 2015   ❤️ 1
    r代表raw就是原始的文字列,不转义。
    popu111
        3
    popu111  
       Jun 18, 2015   ❤️ 1
    原始字符串
    lvfujun
        4
    lvfujun  
    OP
       Jun 18, 2015
    MaiCong
        5
    MaiCong  
       Jun 18, 2015 via iPhone
    http://sebug.net/paper/python/ch04s03.html

    自然字符串

    如果你想要指示某些不需要如转义符那样的特别处理的字符串,那么你需要指定一个自然字符串。自然字符串通过给字符串加上前缀r或R来指定。例如r"Newlines are indicated by \n"。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3227 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 55ms · UTC 13:27 · PVG 21:27 · LAX 06:27 · JFK 09:27
    ♥ Do have faith in what you're doing.