Prévia do material em texto
Figure 3.1 credit: modification of work "Port of Melbourne", by Chris Phutully/Flickr, CC BY 2.0
Chapter Outline
3.1 Strings revisited
3.2 Formatted strings
3.3 Variables revisited
3.4 List basics
3.5 Tuple basics
3.6 Chapter summary
Introduction
An object is a single unit of data in a Python program. So far, this book has introduced three types of objects:
strings, integers, and floats. This chapter takes a closer look at how strings are represented and how integers
and floats can be formatted. To better understand what an object actually is, the relationship between
variables and objects is emphasized. The chapter also introduces two types of containers: lists and tuples. A
container is an object that can hold an arbitrary number of other objects. At the end of this chapter, you will
be able to solve more complex problems using fewer variables.
3.1 Strings revisited
Learning objectives
By the end of this section you should be able to
• Extract a specific character from a string using an index.
• Use escape sequences to represent special characters.
Indexes
A string is a sequence of zero or more characters. Each character has an index that refers to the character's
position. Indexes are numbered from left to right, starting at 0. Indexes are also numbered from right to left,
Objects
3
starting at -1.
CHECKPOINT
String indexes
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
3-1-strings-revisited)
CONCEPTS IN PRACTICE
String indexes
1. What is the index of the second character in a string?
a. 1
b. 2
c. -2
2. If s = "Python!", what is the value of s[1] + s[-1]?
a. "P!"
b. "y!"
c. "yn"
3. If s = "Python!", what type of object is s[0]?
a. character
b. integer
c. string
Unicode
Python uses Unicode, the international standard for representing text on computers. Unicode defines a
unique number, called a code point, for each possible character. Ex: "P" has the code point 80, and "!" has
the code point 33.
The built-in ord() function converts a character to a code point. Ex: ord("P") returns the integer 80.
Similarly, the built-in chr() function converts a code point to a character. Ex: chr(33) returns the string "!".
Unicode is an extension of ASCII, the American Standard Code for Information Interchange. Originally, ASCII
defined only 128 code points, enough to support the English language. Unicode defines over one million code
72 3 • Objects
Access for free at openstax.org
https://openstax.org/books/introduction-python-programming/pages/3-1-strings-revisited
https://openstax.org/books/introduction-python-programming/pages/3-1-strings-revisited
points and supports most of the world's written languages.
32 (space)
33 !
34 "
35 #
36 $
37 %
38 &
39 '
40 (
41 )
42 *
43 +
44 ,
45 -
46 .
47 /
48 0
49 1
50 2
51 3
52 4
53 5
54 6
55 7
56 8
57 9
58 :
59 ;
60 <
61 =
62 >
63 ?
64 @
65 A
66 B
67 C
68 D
69 E
70 F
71 G
72 H
73 I
74 J
75 K
76 L
77 M
78 N
79 O
80 P
81 Q
82 R
83 S
84 T
85 U
86 V
87 W
88 X
89 Y
90 Z
91 [
92 \
93 ]
94 ^
95 _
96 `
97 a
98 b
99 c
100 d
101 e
102 f
103 g
104 h
105 i
106 j
107 k
108 l
109 m
110 n
111 o
112 p
113 q
114 r
115 s
116 t
117 u
118 v
119 w
120 x
121 y
122 z
123 {
124 |
125 }
126 ~
127 (delete)
Table 3.1 Character values. This table shows code points 32 to 127 as defined by ASCII and Unicode. Code
points 0 to 31 are non-printable characters that were used for telecommunications.
CONCEPTS IN PRACTICE
ord() and chr()
4. What is the code point for the letter A?
a. 1
b. 65
3.1 • Strings revisited 73
c. 97
5. What value does ord("0") return?
a. 0
b. 48
c. Error
6. What does chr(126) return?
a. ~
b. "~"
c. Error
Special characters
An escape sequence uses a backslash (\) to represent a special character within a string.
Escape
sequence
Meaning Example Screen output
\n
A newline character that indicates the end
of a line of text.
print("Escape\
nsequence!")
Escape
sequence!
\t
A tab character; useful for indenting
paragraphs or aligning text on multiple
lines.
print("Escape\
tsequence!")
Escape sequence!
\'
A single quote; an alternative to enclosing
the string in double quotes.
print('I\'ll try
my best!')
I'll try my best
\"
A double quote; an alternative to
enclosing the string in single quotes.
print("I heard
you said
\"Yes\"")
I heard you said
"Yes"
\\ A backslash character.
print("This
prints a \\")
This prints a \
Table 3.2 Common escape sequences.
74 3 • Objects
Access for free at openstax.org
CONCEPTS IN PRACTICE
Tabs and newlines
7. Which of the following is an escape sequence?
a. t
b. /t
c. \t
8. Which statement prints a backslash (\) to the screen?
a. print(\\)
b. print(\"\")
c. print("\\")
9. Which statement prints Enter and here on separate lines?
a. print("Enter here")
b. print("Enter" + \n + "here")
c. print("Enter" + "\n" + "here")
TRY IT
Hopper quote
Grace Hopper (https://openstax.org/r/100gracehopper) (1906–1992) was a famous computer scientist (and
rear admiral in the US Navy!) who came up with the idea of machine-independent programming languages.
She envisioned a programming language based on English and made many contributions that paved the
way for modern programming languages, including Python.
Write a program that prints the following text, including the quotation marks. Your program may not use
single quotes (') anywhere in the code. The last line must be indented with a tab character.
"To me programming is more than an important practical art.
It is also a gigantic undertaking in the foundations of knowledge."
-- Grace Hopper
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
3-1-strings-revisited)
TRY IT
Shift cipher
During the Roman Empire, Julius Caesar (100–44 BCE) used a simple technique to encrypt private
messages. Each letter of the message was replaced with the third next letter of the alphabet. Ex: If the
message was CAT, the C became F, the A became D, and the T became W, resulting in the message FDW.
This technique is known as a shift cipher because each letter is shifted by some amount. In Caesar's case,
3.1 • Strings revisited 75
https://openstax.org/r/100gracehopper
https://openstax.org/books/introduction-python-programming/pages/3-1-strings-revisited
https://openstax.org/books/introduction-python-programming/pages/3-1-strings-revisited
Chapter 3 Objects
Introduction
3.1 Strings revisited