What are these "modifiers" called on the front of a python string? I don't understand what these are used for. Also, since I don't know what they are called, I don't know what to search for to learn about them (or the others that may be available if any).
In this example what does the "u" represent on the front of the string in the return?
def __unicode__(self):
return u'title: %s, text: %s, created:%s, tags: %s' % (self.title, self.text, self.created, self.tags)
In this django URL example, what does the "r" represent?
urlpatterns = patterns('',
(r'^admin/',include(admin.site.urls)),
)
I'm learning python and django and I see these in examples, but do not have an explanation of what they represent.
Python strings are "immutable" which means they cannot be changed after they are created (Java strings also use this immutable style). Since strings can't be changed, we construct *new* strings as we go to represent computed values.
%s acts a placeholder for a string while %d acts as a placeholder for a number. Their associated values are passed in via a tuple using the % operator.
The % symbol is used in Python with a large variety of data types and configurations. %s specifically is used to perform concatenation of strings together. It allows us to format a value inside a string.
The 'r' indicates a raw string, which alters the escaping behavior. This is useful for regular expressions to make them easier to read. The 'u' indicates that it is a Unicode string. They're called string literal prefixes.
From the docs:
String literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and use different rules for interpreting backslash escape sequences. A prefix of 'u' or 'U' makes the string a Unicode string. Unicode strings use the Unicode character set as defined by the Unicode Consortium and ISO 10646. Some additional escape sequences, described below, are available in Unicode strings. A prefix of 'b' or 'B' is ignored in Python 2; it indicates that the literal should become a bytes literal in Python 3 (e.g. when code is automatically converted with 2to3). A 'u' or 'b' prefix may be followed by an 'r' prefix.
Unless an 'r' or 'R' prefix is present, escape sequences in strings are interpreted according to rules similar to those used by Standard C.
These differ between Python 2 and Python 3:
Python 2:
"hello" # Normal string (8-bit characters)
u"hello" # Unicode string
r"hello" # Raw string --> Backslashes don't need to be escaped
b"hello" # treated like normal string, to ease transition from 2 to 3
Python 3:
"hello" # Unicode string
b"hello" # Bytes object. Not a string!
r"hello" # Raw string --> Backslashes don't need to be escaped
u"hello" # Python 3.3, treated like Unicode string, to ease transition from 2 to 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With