3 Python Fundamentals m02 Strings Slides
3 Python Fundamentals m02 Strings Slides
‘This is a string’
‘This is a string’
“This is also a string”
Practicality beats
purity
Beautiful text strings
Rendered in literal form
Simple elegance
1. Multiline strings
1. Multiline strings
2. Escape sequences
‘\n’
‘\n’
P EP 2 7 8
p e ps / pe p - 02 78 /
y th o n . o rg / d e v /
www. p
r g /3 / r e fe r e n c e /
\r
d o c s .p y th o n .o
ASCII Carriage Return (CR)
g s
\t
n
ASCII Horizontal Tab (TAB)
\v
a l y s i s. h tm
ASCII Vertical Tab (VT)
l #s tr i
\ooo
\xhh lexical_ a n
Character with octal value ooo
Character with hex value hh
Only recognized in string literals
\N{name} Character named name in the Unicode database
\uxxxx Character with 16-bit hex value xxxx
\Uxxxxxxxx Character with 32-bit hex value xxxxxxxx
}
‘a long string’
str
‘x’
}
‘a long string’
str
‘x’
l y o n e - e le m e nt
rac ter s” a re s i m p
“c h a
strings
Friday, July 26, 13
Python Strings Are Unicode
UT F - 8 l i te ra l s
b‘data’
b“data”
decode
str
bytes encode
decode
str
bytes encode
Encodings
m l#s t a n da rd- e nco d ing s
ra r y / cod e c s.ht
docs.python.org/3/lib
Friday, July 26, 13
Friday, July 26, 13
list
mutable sequences of objects
[a, b, c, d]
keys
{k1: v1, k2: v2}
keys
{k1: v1, k2: v2}
values
Friday, July 26, 13
for-loop visit each item in a sequence
Ctrl-Z
Ctrl-D
b‘data’
b“data”
decode
str
bytes encode
[a, b, c, d]
[a, b, c, d]
[a, b, c, d]
[a, b, c, d]
[a, b, c, d]
[a, b, c, d]
[a, b, c, d]