0% found this document useful (0 votes)
65 views

3 Python Fundamentals m02 Strings Slides

This document discusses Python fundamentals related to strings and collections. It covers string literals and how to represent strings with newlines or special characters. It also discusses bytes literals and how to convert between strings and bytes. Additionally, it introduces common mutable and immutable collection types like lists, dictionaries, and their literal syntax. It demonstrates how to iterate over items in a sequence using a for loop.

Uploaded by

VFisa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

3 Python Fundamentals m02 Strings Slides

This document discusses Python fundamentals related to strings and collections. It covers string literals and how to represent strings with newlines or special characters. It also discusses bytes literals and how to convert between strings and bytes. Additionally, it introduces common mutable and immutable collection types like lists, dictionaries, and their literal syntax. It demonstrates how to iterate over items in a sequence using a for loop.

Uploaded by

VFisa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 76

Python Fundamentals

Strings and Collections

Austin Bingham Robert Smallshire


@austin_bingham @robsmallshire
[email protected] [email protected]
Presenter

Friday, July 26, 13


Python
Collections

Friday, July 26, 13


Python
Collections

Friday, July 26, 13


Python
Collections
str

Friday, July 26, 13


Python
Collections
str
bytes

Friday, July 26, 13


Python
Collections
str
bytes
list

Friday, July 26, 13


Python
Collections
str
bytes
list
dict
Friday, July 26, 13
Python
Collections
str
bytes
list
p s
dict or - loo
f
Friday, July 26, 13
str
immutable sequences of Unicode codepoints

Friday, July 26, 13


String Literals

‘This is a string’

Friday, July 26, 13


String Literals

‘This is a string’
“This is also a string”

Friday, July 26, 13


Moment of Zen

Practicality beats
purity
Beautiful text strings
Rendered in literal form
Simple elegance

Friday, July 26, 13


Strings with Newlines

Friday, July 26, 13


Strings with Newlines

1. Multiline strings

Friday, July 26, 13


Strings with Newlines

1. Multiline strings

2. Escape sequences

Friday, July 26, 13


‘\r\n’
or
‘\n’
?
Friday, July 26, 13
Universal Newlines

‘\n’

Friday, July 26, 13


Universal Newlines

‘\n’
P EP 2 7 8
p e ps / pe p - 02 78 /
y th o n . o rg / d e v /
www. p

Friday, July 26, 13


Escape Sequences
Sequence Meaning
\newline Backslash and newline ignored
\\ Backslash (\)
\’ Single quote (‘)
\a ASCII Bell (BEL)
\b ASCII Backspace (BS)
\f ASCII Formfeed (FF)
\n ASCII Linefeed (LF)
\r ASCII Carriage Return (CR)
\t ASCII Horizontal Tab (TAB)
\v ASCII Vertical Tab (VT)
\ooo Character with octal value ooo
\xhh 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

Friday, July 26, 13


Escape Sequences
Sequence Meaning
\newline Backslash and newline ignored
\\ Backslash (\)
\’ Single quote (‘)
\a ASCII Bell (BEL)
\b ASCII Backspace (BS)
\f ASCII Formfeed (FF)
\n ASCII Linefeed (LF)

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

Friday, July 26, 13


No Separate Character Type

}
‘a long string’
str
‘x’

Friday, July 26, 13


No Separate Character Type

}
‘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

Friday, July 26, 13


Python Strings Are Unicode

UT F - 8 l i te ra l s

Friday, July 26, 13


bytes
immutable sequences of bytes

Friday, July 26, 13


Bytes Literals

b‘data’
b“data”

Friday, July 26, 13


Converting Between Strings and Bytes

decode
str

bytes encode

Friday, July 26, 13


Converting Between Strings and Bytes

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

Friday, July 26, 13


List Literals

[a, b, c, d]

Friday, July 26, 13


dict
mutable mappings of keys to values

Friday, July 26, 13


Dict Literals

{k1: v1, k2: v2}

Friday, July 26, 13


Dict Literals

keys
{k1: v1, k2: v2}

Friday, July 26, 13


Dict Literals

keys
{k1: v1, k2: v2}

values
Friday, July 26, 13
for-loop visit each item in a sequence

Friday, July 26, 13


For-Loop Syntax

for ITEM in SEQUENCE:


...body...

Friday, July 26, 13


Exit the REPL

Ctrl-Z

Ctrl-D

Friday, July 26, 13


o g e t h er
g i t all t
P u  in

Friday, July 26, 13


o g e t h er
g i t a ll t
P u  in
l o pe n( )
r l l i b. u r
• u

Friday, July 26, 13


o g e t h er
g i t a ll t
P u  in
l o pe n( )
r l l i b. u r
• u
t a t e m e n t
h - s
• wit

Friday, July 26, 13


o g e t h er
g i t a ll t
P u  in
l o pe n( )
r l l i b. u r
• u
t a t e m e n t
h - s
• wit
• list

Friday, July 26, 13


o g e t h er
g i t a ll t
P u  in
l o pe n( )
r l l i b. u r
• u
t a t e m e n t
h - s
• wit
• list op
•f o r - lo

Friday, July 26, 13


o g e t h er
g i t a ll t
P u  in
l o p e n ( )
r l l i b. u r
• u
t a t e m e n t
h - s
• wit
• list op
•f o r - lo
. s p l i t ( )
• b y t e s

Friday, July 26, 13


Recall: Bytes Literals

b‘data’
b“data”

Friday, July 26, 13


Recall: Converting Between Strings and
Bytes

decode
str

bytes encode

Friday, July 26, 13


o g e t h er
g i t a ll t
P u  in
l o p e n ( )
r l l i b. u r
• u
t a t e m e n t
h - s
• wit
• list op
•f o r - lo
. s p l i t ( )
• b y t e s )
e c o d e (
y t e s . d
•b

Friday, July 26, 13


o g e t h er
g i t a ll t
P u  in
l o p e n ( )
r l l i b. u r
• u
t a t e m e n t
h - s
• wit
• list op
•f o r - lo
. s p l i t ( )
• b y t e s )
e c o d e (
y t e s . d
•b l it ( )
t r . s p
•s

Friday, July 26, 13


Summary: Strings and Bytes

Friday, July 26, 13


Summary: Strings and Bytes
 Single- and multi-line string quoting

Friday, July 26, 13


Summary: Strings and Bytes
 Single- and multi-line string quoting
 Adjacent string literal concatenation

Friday, July 26, 13


Summary: Strings and Bytes
 Single- and multi-line string quoting
 Adjacent string literal concatenation
 Universal newlines

Friday, July 26, 13


Summary: Strings and Bytes
 Single- and multi-line string quoting
 Adjacent string literal concatenation
 Universal newlines
 Escape sequences for control characters

Friday, July 26, 13


Summary: Strings and Bytes
 Single- and multi-line string quoting
 Adjacent string literal concatenation
 Universal newlines
 Escape sequences for control characters
 Raw strings suppress the escaping mechanism

Friday, July 26, 13


Summary: Strings and Bytes
 Single- and multi-line string quoting
 Adjacent string literal concatenation
 Universal newlines
 Escape sequences for control characters
 Raw strings suppress the escaping mechanism
 Convert other types with the str() constructor

Friday, July 26, 13


Summary: Strings and Bytes
 Single- and multi-line string quoting
 Adjacent string literal concatenation
 Universal newlines
 Escape sequences for control characters
 Raw strings suppress the escaping mechanism
 Convert other types with the str() constructor
 Zero-based square-bracket indexing of strings

Friday, July 26, 13


Summary: Strings and Bytes
 Single- and multi-line string quoting
 Adjacent string literal concatenation
 Universal newlines
 Escape sequences for control characters
 Raw strings suppress the escaping mechanism
 Convert other types with the str() constructor
 Zero-based square-bracket indexing of strings
 Rich variety of string methods

Friday, July 26, 13


Summary: Strings and Bytes
 Single- and multi-line string quoting
 Adjacent string literal concatenation
 Universal newlines
 Escape sequences for control characters
 Raw strings suppress the escaping mechanism
 Convert other types with the str() constructor
 Zero-based square-bracket indexing of strings
 Rich variety of string methods
 Python 3 source encoding is UTF-8I

Friday, July 26, 13


Summary: Strings and Bytes
 Single- and multi-line string quoting
 Adjacent string literal concatenation
 Universal newlines
 Escape sequences for control characters
 Raw strings suppress the escaping mechanism
 Convert other types with the str() constructor
 Zero-based square-bracket indexing of strings
 Rich variety of string methods
 Python 3 source encoding is UTF-8I
 bytes is a sequence of bytes, str is a sequence of Unicode codepoints

Friday, July 26, 13


Summary: Strings and Bytes
 Single- and multi-line string quoting
 Adjacent string literal concatenation
 Universal newlines
 Escape sequences for control characters
 Raw strings suppress the escaping mechanism
 Convert other types with the str() constructor
 Zero-based square-bracket indexing of strings
 Rich variety of string methods
 Python 3 source encoding is UTF-8I
 bytes is a sequence of bytes, str is a sequence of Unicode codepoints
 bytes literals prefixed with a lowercase b

Friday, July 26, 13


Summary: Strings and Bytes
 Single- and multi-line string quoting
 Adjacent string literal concatenation
 Universal newlines
 Escape sequences for control characters
 Raw strings suppress the escaping mechanism
 Convert other types with the str() constructor
 Zero-based square-bracket indexing of strings
 Rich variety of string methods
 Python 3 source encoding is UTF-8I
 bytes is a sequence of bytes, str is a sequence of Unicode codepoints
 bytes literals prefixed with a lowercase b
 Convert str to bytes with encode(), bytes to str with decode()

Friday, July 26, 13


Summary: Lists

[a, b, c, d]

Friday, July 26, 13


Summary: Lists
 Lists are mutable, heterogeneous sequences of objects

[a, b, c, d]

Friday, July 26, 13


Summary: Lists
 Lists are mutable, heterogeneous sequences of objects
 List literals delimited by square brackets, items separated by commas

[a, b, c, d]

Friday, July 26, 13


Summary: Lists
 Lists are mutable, heterogeneous sequences of objects
 List literals delimited by square brackets, items separated by commas
 Zero-based, square-bracket indexing to retrieve objects

[a, b, c, d]

Friday, July 26, 13


Summary: Lists
 Lists are mutable, heterogeneous sequences of objects
 List literals delimited by square brackets, items separated by commas
 Zero-based, square-bracket indexing to retrieve objects
 Square-bracket assignment to replace objects

[a, b, c, d]

Friday, July 26, 13


Summary: Lists
 Lists are mutable, heterogeneous sequences of objects
 List literals delimited by square brackets, items separated by commas
 Zero-based, square-bracket indexing to retrieve objects
 Square-bracket assignment to replace objects
 Grow lists with append()

[a, b, c, d]

Friday, July 26, 13


Summary: Lists
 Lists are mutable, heterogeneous sequences of objects
 List literals delimited by square brackets, items separated by commas
 Zero-based, square-bracket indexing to retrieve objects
 Square-bracket assignment to replace objects
 Grow lists with append()
 Construct from other sequences using list() constructor

[a, b, c, d]

Friday, July 26, 13


Summary: Dictionaries

{k1: v1, k2: v2}

Friday, July 26, 13


Summary: Dictionaries
 Dictionaries associate keys with values

{k1: v1, k2: v2}

Friday, July 26, 13


Summary: Dictionaries
 Dictionaries associate keys with values
 Literal dicts delimited by curly braces

{k1: v1, k2: v2}

Friday, July 26, 13


Summary: Dictionaries
 Dictionaries associate keys with values
 Literal dicts delimited by curly braces
 Literal key-value pairs separated by commas, with a colon between each
key and value

{k1: v1, k2: v2}

Friday, July 26, 13


Summary: For-loops

for ITEM in SEQUENCE:


...body...

Friday, July 26, 13


Summary: For-loops

 Take items one-by-one from an iterable object, binding a name to the


current item

for ITEM in SEQUENCE:


...body...

Friday, July 26, 13


Summary: For-loops

 Take items one-by-one from an iterable object, binding a name to the


current item
 Correspond to for-each loops in other languages

for ITEM in SEQUENCE:


...body...

Friday, July 26, 13


Summary: For-loops

 Take items one-by-one fromNext


an iterable object, binding a name to the
time in
current item Python Fundamentals
 Correspond to for-each loops in other languages
functions
modules
execution model
for ITEM in SEQUENCE: command-line
arguments
...body...

Friday, July 26, 13

You might also like