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, starting at -1.
Concepts in Practice
String indexes
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 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) |
Concepts in Practice
ord() and chr()
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!") |
|
\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 \ |
Concepts in Practice
Tabs and newlines
Try It
Hopper quote
Grace Hopper (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
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, the amount was three, but other amounts (besides 0) would work too.
Write a program that prompts the user to input the following two values (example input in bold):
Enter a 3-letter word: CAT Shift by how many letters? 3
The program should then shift each letter of the word by the desired amount. Based on the example above, the output would be:
The secret message is: FDW
Hint: Use the ord()
function to convert each letter to an integer, add the shift amount to each integer, use the chr()
function to convert each integer to a character, and concatenate the resulting characters.