Numerical literals
A numerical value can be expressed in its hexadecimal representation by prefixing it with 0x
>>> 0xdeadbeef
3735928559
Similarly, a numerical value can also be expressed as in octal representation by prefixing it with 0o (the first character is a zero, the second one an upper or lowercase o).
>>> 0o777
511
Same idea, but as
byte representation (
0b…, compare with
b'…' below). The following expression calculates 4*3:h
0b100 * 0b0011
In numerical literals, underscores can be used to make them more readable by a human eye. Such underscores are simply skipped by the parser.
>>> print(21_000_000_000 / 7_000_000)
3000.0
A number that is suffixed with a j becomes a complex number:
>>> type(4.2j)
<class 'complex'>
String literals
Strings are either single- or double-quoted.
Strings in double quotes. Note how two strings are automatically concatenated:
>>> print("foo\nbar\\baz" " x\"y")
foo
bar\baz x"y
Strings in single quotes:
>>> print('foo\nbar\\baz' ' x\'y')
foo
bar\baz x'y
R-Strings
With R-strings, a backslash has no special meaning and represents itself. The R of the R-String stands for raw.
>>> print(r"foo\bar")
foo\bar
An R-string cannot have a trailing slash!
>>> print(r"foo\bar\")
…
SyntaxError: EOL while scanning string literal
A trailing slash can be added by the automatic string concatenation:
>>> print(r"foo\bar" "\\")
foo\bar\
f-Strings
An
f-string replaces
expressions within curly braces with the values the evaluate to.
The f in f-strings stands for formatted string (see also the
format method of the
string object).
>>> a=7
>>> b=6
>>> print(f'{a} * {b} = {a*b}')
7 * 6 = 42
In f-strings, a number can be printed with a comma as thousand separator like so:
>>> cost_per_item = 19_999
>>> items = 61
>>> print(f'Total cost: {cost_per_item * items:,}')
Total cost: 1,219,939
Left- and right-aligning data (for columnar output)
data = [("Alice", 25, "USA"), ("Bob", 7, "UK")]
print(f"{'Name':<10}{'Age':<5}{'Country':<10}")
for name, age, country in data:
print(f"{name:<10} {age:>2} {country:<3}")
Dynamic widths:
wid = 6
val = 123
print(f"Value: {val:{wid}}") # Value: 123
A variable followed by an equal sign (=) expands both, the variable name and its value:
>>> var = 42
>>> print(f'{var=}')
var=42
In an f-string, a curly brace must be escaped by itself:
print(f'{{ within ONE pair of curly braces }}')
Multi-line strings
«Normal» strings cannot have
new lines in them:
>>> print('first line
^
SyntaxError: unterminated string literal (detected at line 1)
In order to create multi line strings, the string must be embedded in «triple quotes» (single or double):
>>> txt = """foo
... bar\n
... baz"""
>>>
>>> print(txt)
foo
bar
baz
That said, Python recognizes \n and \r in string literals:
print('one\ntwo\nthree')
print('ABCDE world\rhello')
Byte string literals (b-strings)
b'…' creates a
bytes object:
>>> name = b'Ren\xc3\xa9'
>>> type(name)
<class 'bytes'>
>>> print(name.decode('utf-8'))
René
The characters in a b-string must be either ASCII or escaped hexadecimal values:
>>> print(b'ähm')
SyntaxError: bytes can only contain ASCII literal characters