Text And Mutability

Submitted By lilgirlsrule
Words: 1419
Pages: 6

CMPT 103
Text and Mutability

Sources:
Chapters 8 and 9 from Think Python and Ch. 10 of Guzdial.

www.macewan.ca/ComputerScience

In these slides…
• Text
• Types of objects
– Mutable
– Immutable

• Strings
– Methods

• Lists
– Methods www.macewan.ca/ComputerScience 2

Text
• Text is the universal medium – it's simple – We can convert any other media to a text representation
– We can convert between media formats using text

• In programming languages (not just
Python), text is usually processed in an array—a long line of characters www.macewan.ca/ComputerScience 3

Text
• We refer to a long line of characters as a string
– In many (especially older) programming languages, text is actually manipulated as arrays (or list) of characters
– Python actually knows how to deal with strings www.macewan.ca/ComputerScience

4

Strings
• Strings are defined with quote marks
• Python supports 3 quote types of quotes: 1. print('this is a string')
2. print("this is a string")
3. print ("""this is a string""")

• Use the right one that allows you to embed quote marks you want, e.g.,
"It's a great day." www.macewan.ca/ComputerScience 5

Triple quotes
• Triple quotes allow you to quite naturally embed new lines within strings def display_greeting(): print("""Hello, world!
How are you today?""")

www.macewan.ca/ComputerScience

6

Under the hood
• Strings are just arrays of characters
• In most cases, characters are just single bytes
– The ASCII encoding standard maps between single byte values and the corresponding characters

www.macewan.ca/ComputerScience

7

Under the hood
• More recently, characters can be multiple bytes
– Unicode uses one to four bytes per character, allowing encodings for glyphs
(characters) of other languages
– Python 3 encodes all characters in
Unicode by default; in the programs we're writing, strings are actually using Unicode
• Python 2 used ASCII

www.macewan.ca/ComputerScience

8

ASCII encoding through ord str = "Hello" for char in str: print(ord(char))
Output:
72
101
108
108
111 www.macewan.ca/ComputerScience 9

Characters than we cannot type • Our keyboards don't have all the characters available to us, and it's hard to type others into strings, e.g.,
– Backspace?
– Return?
– 你好 ?

• We use backslash escapes to include other characters in strings www.macewan.ca/ComputerScience 10

Backslash escapes





"\b" backspace "\n" newline (Enter key)
"\t"
tab
"\uXXXX" a Unicode character
– XXXX is a code; each X can be 0-9 or A-F.
– http://www.unicode.org/charts/

www.macewan.ca/ComputerScience

11

Testing strings print("hello\t\tthere\nMark") hello there Mark print("\u4F60") 你 print("\u597D") 好 print("This\bis\na\btest") This␈ is
Thiis
a␈ test test (Wing IDE)
(Python in Terminal)

www.macewan.ca/ComputerScience

12

Manipulating strings
• We can add strings and get their lengths using the kinds of programming features we've seen previously hello = "Hello" print(len(hello)) 5 mark = ", Mark" print(len(mark)) 6 print(hello+mark) Hello, Mark print(len(hello+mark)) 11

www.macewan.ca/ComputerScience

13

Getting parts of strings
• We use the square bracket "[]" notation to get parts of strings
– string[n]
• gives you the nth character in the string

– string[n:m]
• gives you the nth up to (but not including) the mth character

www.macewan.ca/ComputerScience

14

Getting parts of strings hello = "Hello" print(hello[1]) e print(hello[0]) H print(hello[2:4]) ll

H

e

l

l

o

0

1

2

3

4

www.macewan.ca/ComputerScience

15

Omitting start/end print(hello) Hello print(hello[:3]) Hel print(hello[3:]) lo print(hello[:]) Hello www.macewan.ca/ComputerScience 16

Dot notation
• All data in Python are actually objects
• Objects not only store data, but they respond to special functions that only objects of the same type understand
• We call these special functions methods – Methods are functions known only to certain objects

www.macewan.ca/ComputerScience

17

Dot notation
• To execute a