zoukankan      html  css  js  c++  java
  • What does an 'r' represent before a string in python? [duplicate]

    What does an 'r' represent before a string in python? [duplicate]

    r means the string will be treated as raw string.

    From here:

    When an 'r' or 'R' prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string. For example, the string literal r" " consists of two characters: a backslash and a lowercase 'n'. String quotes can be escaped with a backslash, but the backslash remains in the string; for example, r""" is a valid string literal consisting of two characters: a backslash and a double quote; r"" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line continuation.

    What exactly do “u” and “r” string flags do, and what are raw string literals?

    While asking this question, I realized I didn't know much about raw strings. For somebody claiming to be a Django trainer, this sucks.

    I know what an encoding is, and I know what u'' alone does since I get what is Unicode.

    • But what does r'' do exactly? What kind of string does it result in?

    • And above all, what the heck does ur'' do?

    • Finally, is there any reliable way to go back from a Unicode string to a simple raw string?

    • Ah, and by the way, if your system and your text editor charset are set to UTF-8, does u'' actually do anything?

    回答:

    There's not really any "raw string"; there are raw string literals, which are exactly the string literals marked by an 'r' before the opening quote.

    A "raw string literal" is a slightly different syntax for a string literal, in which a backslash, , is taken as meaning "just a backslash" (except when it comes right before a quote that would otherwise terminate the literal) -- no "escape sequences" to represent newlines, tabs, backspaces, form-feeds, and so on. In normal string literals, each backslash must be doubled up to avoid being taken as the start of an escape sequence.

    This syntax variant exists mostly because the syntax of regular expression patterns is heavy with backslashes (but never at the end, so the "except" clause above doesn't matter) and it looks a bit better when you avoid doubling up each of them -- that's all. It also gained some popularity to express native Windows file paths (with backslashes instead of regular slashes like on other platforms), but that's very rarely needed (since normal slashes mostly work fine on Windows too) and imperfect (due to the "except" clause above).

    r'...' is a byte string (in Python 2.*), ur'...' is a Unicode string (again, in Python 2.*), and any of the other three kinds of quoting also produces exactly the same types of strings (so for example r'...'r'''...'''r"..."r"""...""" are all byte strings, and so on).

    Not sure what you mean by "going back" - there is no intrinsically back and forward directions, because there's no raw string type, it's just an alternative syntax to express perfectly normal string objects, byte or unicode as they may be.

    And yes, in Python 2.*, u'...' is of course always distinct from just '...' -- the former is a unicode string, the latter is a byte string. What encoding the literal might be expressed in is a completely orthogonal issue.

    E.g., consider (Python 2.6):

    >>> sys.getsizeof('ciao')
    28
    >>> sys.getsizeof(u'ciao')
    34
    

    The Unicode object of course takes more memory space (very small difference for a very short string, obviously ;-).

  • 相关阅读:
    技术省钱野路子!轻松节省90%云端开销
    免费服务器迁移上云实践分享!一键迁云,自动同步
    云上自建数据库,秒级备份,看这篇就对了!
    最佳实践 | 弹性计算Region化部署和跨可用区容灾
    云服务器无法远程连接?4步排查,准能解决!
    阿里云弹性计算安全组最佳实践及新特性介绍
    AI云原生浅谈:好未来AI中台实践
    最佳实践 | 基于弹性计算网络能力提升容器密度
    AI性能最高提升20倍 阿里云新一代GPU云服务器亮相 搭载NVIDIA A100
    【转】【用户状态】详细解读Oracle用户ACCOUNT_STATUS的九种状态
  • 原文地址:https://www.cnblogs.com/chucklu/p/14126039.html
Copyright © 2011-2022 走看看