What is the difference between 'string' and r'string in Python?

Asked

Viewed 1,835 times

10

I was taking a look at Django’s code, framework in Python, and I came across the following code in the file urls.py.

urlpatterns = [
    url(r'^articles/2003/$', views.special_case_2003),
    url(r'^articles/([0-9]{4})/$', views.year_archive),
    url(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),
    url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),
]

I didn’t really understand what that passage would be

r'^articles/2003'

What is this r preceding the declaration of string?

It is something related to regular expressions?

  • 1

    Honestly, I do not know what the problem has the question and why received -1

  • 1

    I don’t care, @gmsantos. Let it go. Long live Stackoverflow!

2 answers

11


A better example would be (yours makes no difference using the r):

r'^articles\2003'

This works the way it is. The r indicates that the string is crude, it is so and does not consider special characters. Without the r in the literal the backslash (backslash) would make what follows to be considered a special character. Then it would be considered an octal 200 in this case and then the digit 3. The format \nnn is a character represented in octal string "normal". Obviously all those control characters with the backslash are also ignored.

How soon will you ask, u can also be used to represent strings in Unicode encoding.

Double quotes do not change anything, unlike PHP. But triple quotes ""text""" have difference, that is another matter.

  • But wouldn’t this be done simply using a double-deck instead of the simple? It’s not like that in Python?

  • Man, interesting! Could I ask another question?! Why there seems to be no difference between ' and " in python!

  • That’s it, there’s no difference, it’s like using one or the other in Python.

  • The failure of Cloudflare is very boring!!!!! , hinders in the worst hours!

9

What is this r that precedes the string declaration?

The r before the quotes comes from raw, that is, the string will be interpreted as a literal string.

In a raw string, escape characters such as \n, \r, \t and others are not processed.

# No primeiro caso, o trecho \123 será exibido ao invés de ser convertido
# para a representação octal da letra S
raw = r"\directory\123"
val = "\directory\123"

print(raw)
print(val)

Exit:

\directory 123

\directoryS

The behavior is similar to PHP single quotes and the @ in the C#.


It is something related to regular expressions?

It is not directly related, but regular expressions use \ to escape special characters (which are diverse). Since some expressions may be conflicting, it is preferable to always use literal strings when working with regex.

  • Vixi, now confused me. So what would be the difference between "string", r"string", 'string'e'r'string'`?

  • Apparently there’s no difference ' and " in Pyhton - http://stackoverflow.com/questions/56011/single-quotes-vs-double-quotes-in-python

  • Now it’s more than understood. Interesting is that it has nothing to do with regexp. I thought the r era of regexp

  • @Wallacemaxters edited the answer. I had forgotten the second question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.