55284A - Introduction to Python _ Chapter4
55284A - Introduction to Python _ Chapter4
Topics Covered
String basics.
SpecialTcharacters.
his
do
cu
Multi-line strings.me
nt
be
lon
ja
Indexing and Nslicinggstrings. g
ou tap s to
na n N
uth ilkan ILKA
Common string operators o t
riz and@methods. N
ed red TES
co if H
pie f.com WAR
Formatting strings. sa JA
llo GT
we AP
d! .
Built-in string functions.
‘Yes,’ they say to one another, these so kind ladies, ‘he is a stupid old fellow, he will see not what we do,
he will never observe that his sock heels go not in holes any more, he will think his buttons grow out new
T
when hthey
is fall, and believe that strings make theirselves.’
do
cu
– Little Women, me Louisa May Alcott
nt
be
lon
No agta gs to
j
u p N
Introduction nautho nilkant@ILKAN
riz re TE
ed SH
co diff.c
pie om WAR
According to the Python documentation s a 17, “StringsJAare immutable sequences of Unicode code points.” Less
llo GT
w AP18 The term sequence in Python refers to an ordered set.
technically speaking, strings are sequenceseof d! characters. .
Other common sequence types are lists, tuples, and ranges, all of which we will cover.
Likewise, to create a string that contains a double quote (e.g., The soldier asked, "Are you suggesting
coconuts migrate?"), enclose the string in single quotes:
Th
is
do
cu
me
nt
phrase = 'The soldier be asked, "Are you suggesting coconuts migrate?"'
lon
N ja g gs
Sometimes you will want to output single quotes within a string denoted by single quotes or double quotes within a
string denoted by double quotes. In such cases, you will need to escape the quotation marks using a backslash (\),
like this:
>>> phrase = "The soldier said, \"You've got two empty halves of a coconut.\""
>>> print(phrase)
T
sd hi
The soldier said,
oc "You've got two empty halves of a coconut."
u me
nt
be
# or lon
No ja gta gs to
un p NI
au nilka L
tho nt@ KAN
>>> phrase = 'The soldier riz said, TE
red "You\'ve got two empty halves of a coconut."'
ed SH
c op iff . W
>>> print(phrase) ies com AR
a JA
llo empty halves
The soldier said, "You've got two we GT of a coconut."
d! AP
.
Notice that the printed output does not contain the backslashes.
Special Characters
Th
The backslashiscan
do also be used to escape characters that have special meaning, such as the backslash itself:
cu
me
nt
be
lon
No agta gs to
j
>>> phrase = "Use unan extra
p backslash
NI to output a backslash: \\"
au nilka LK
tho n A
>>> print(phrase)
riz t@re NTE
ed d SH
co if a backslash:
pie f.com WAR
Use an extra backslash to output \
sa JA
llo GT
we AP
d! .
Two other common special characters are the newline (\n) and horizontal tab (\t):
\\ Backslash
\n Newline
Th
is
\t do Horizontal tab
cu
m en
tb
elo
Raw Strings jag ng
s
N
Sometimes, a string might have a lot of backslashes in it that are just meant to be plain old backslashes. The most
common example is a file path. For example:
'C:\news\today.txt'
Using the “r” (for raw data) prefix on the string, we ensure that all backslashes are escaped:
Th
is
d
>>> my_path = or'C:\news\today.txt'
cu
m
n e
>>> print(my_path) t be
lon
No jagta gs to
C:\news\today.txt
un pn NI
auil LK
tho kant
riz @ ANT
ed red E
iff. SHW
co without
If you examine the variable directly pie c o printing
AR it, you see that each backslash is escaped with another
sa m JA
backslash: llo GT
we AP
d! .
>>> my_path
'C:\\news\\today.txt'
Th
is
do
Note that backslashescu will always escape single and double quotation marks, even in a raw string, so you cannot
m
end a raw string with aensingle
tb backslash:
elo
n
No jagta gs to
un pn NI
au il LK
tho kant
Bad riz @ ANT
ed re ES
my_path = r'C:\my\new\' co diff.c HW
pie o AR
sa m JA
llo GT
we AP
d! .
Good
my_path = r'C:\my\new\\'
Raw strings can come in particularly handy when working with regular expressions.
Th
is
do
Triple Quotes cu
me
nt
be
lon
N ja g gs
Triple quotes are used to create multi-line strings. You generally use three double quotes 19 as shown in the
following example:
1. print("""----------------
2. LUMBERJACK
T SONG
his
3. do
cu
en m
4. tb
I'm a lumberjack
elo
n
5. No jagta gs to
And I'm O.K.
6. na pnilk NIL
I sleep all unight
u Ktho an
riz t@ ANT
d c rediff ESH
7. And I work all day. e
8. ----------------""") opie .com
WA
sa R
llo JA
we GT
d! AP
.
Code Explanation
Th
is
oc d
----------------
um
en
LUMBERJACK SONG tb
elo
n
No agta gs to
j
un pn NI
au il LK
I'm a lumberjack tho kant
riz @ ANT
And I'm O.K. ed re ES
co diff.c HW
pie o AR
I sleep all night sa m JA
llo GT
And I work all day. we AP
d! .
----------------
Note that quotation marks can be included within triple-quoted strings without being escaped with a backslash.
Th
In some cases iswhen using triple quotes, you may want to break up your code with a newline without having that
do
c
newline show up in uyour
me output. You can escape the actual newline with a backslash as shown in the following
nt
demo: be
lon
No jagta gs to
un p NI
au nilka L
nt@ KAN
Demo 17: strings/Demos/triple_quotes_newline_escaped.py
tho
riz r e TE
ed SH
co diff.c
pie om WAR
sa JA
1. print("""We're knights of the llo Round Table,
GT \
we AP
2. we dance whene'er we're able. d ! .
3. We do routines and chorus scenes \
4. with footwork impeccable,
5. We dine well here in Camelot, \
6. we eat ham and jam and Spam a lot.""")
Th
is
do
cu
men
Code Explanation tb
elo
jag ng
N s
The preceding code will render the following:
Th
is
do
cu
Notice that the backslashes at the end of lines 1, 3, and 5 prevent line breaks in the output.
me
nt
be
lon
ja g
String Indexing N ou g tap s to
na n N
uth ilkan ILKA
ori t@ NT
ze
d c rediff ESH
Indexing is the process of finding aopspecific . WA within a sequence of elements through the element’s position.
ies comelement R
Remember that strings are basically sequences all JA
of characters. We can use indexing to find a specific character
ow GT
ed AP
within the string. ! .
If we consider the string from left to right, the first character (the left-most) is at position 0. If we consider the string
from right to left, the first character (the right-most) is at position -1. The following table illustrates this for the
phrase “MONTY PYTHON”.
Th
is
do
cu
me M O N T Y P Y T H O N
nt
be
lon
Left to Right:
No jagta gs to 0 1 2 3 4 5 6 7 8 9 10 11
un pn N IL
au il
Right to Left: tho kant -12 KAN -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
riz @ T
ed re E SH
co diff.c WA
The following demonstration shows phow
ies toom find characters
R by position using indexing.
all JA
ow GT
ed AP
Demo 18: strings/Demos/string_indexing.py ! .
Th
Code Explanation
is
do
cu
m
en
The expected output for teach
be print statement is shown in square brackets in the comment. Running the file
lon
should result in: N ja g gs
M
n
y
h
Th
is
do
Exercise cu 11: Indexing Strings
me
nt
be
lon
No ja gta gs to
un p NI
au nilka L
10 to 20 minutes tho nt@ KAN
riz re TE
ed SH
co diff.c
pie om WAR
sa JA
llo GT
In this exercise, you will write a program that we gets a specific
AP character from a phrase entered by the user.
d! .
1. Open strings/Exercises/indexing.py.
A. Prompts
Th the user to enter a phrase.
is
do
c
B. Tells the uuser me what phrase they entered (e.g., Your phrase is 'Hello, World!').
nt
be
lon
C. PromptsNthe user ja g a number.
for
o u gtap s to N
na n
uth ilkan ILKA
D. Tells the user what t
ori character N
is at that position in the user’s phrase (e.g., Character number 4 is
ze @red TES
o). dc iff HW
op .
ies com AR
all JA
ow GT
3. Here is the program completed by the euser: d! AP
.
Solution: strings/Solutions/indexing.py
1.
T
his
def main():
do
2. phrasecum
= input("Choose a phrase: ")
en
t
3. print("Yourbephrase
lo is '", phrase, "'", sep="")
ja ng
gta st
pos = Nint(input("Which
o
4.
un p o character? [Enter number] "))
NI
au nilka LK
tho
@ ANT
nnumber",
5. print("Character t pos, "is", phrase[pos])
riz red ES
ed H
6. co if
pie f.com WAR
7. main() sa JA
llo GT
we AP
d! .
1. def main():
T his
2. do = input("Choose a phrase: ")
phrasec um
3. en phrase is '", phrase, "'", sep="")
print("Your
t be
lon
4. ja gs
pos = int(input("Which
gta No
character? [Enter number] "))-1
to
5. na pnilk ",
print("Character u NI pos+1, " is '", phrase[pos], "'", sep="")
uth an LKA
ori t N
6. ze @red TES
dc iff HW
op .
7. main() ies com AR
all JA
ow GT
ed AP
! .
Slicing Strings
Often, you will want to get a sequence of characters from a string (i.e., a substring). In Python, you do this by
getting a slice of the string using the following syntax:
Th
is
do
cu
me
nt
be
substring = orig_string[first_pos:last_pos]
lon
No jagta gs to
un p NI
au nilka L
tho nt@ KAN
This returns a slice that starts withr i z r TE at first_pos and includes all the characters up to but not
ed theedcharacter
co iff. SHW
including the character at last_pos. pie c om AR
sa JA
llo GT
we AP
d! .
If first_pos is left out, then it is assumed to be 0. So 'hello'[:3] would return "hel".
If last_pos is left out, then it is assumed to be the length of the string, or in other words, one more than the last
position of the string. So 'hello'[3:] would return "lo".
T
The following hdemonstration
is
do shows how to get substrings using slicing.
cu
me
nt
be
Demo 19: strings/Demos/string_slicing.py
lon
N ja g gs
1. phrase = "Monty Python"
2.
3. first_5_letters = phrase[0:5] # [Monty] Python
4. print(first_5_letters)
5.
6. letters_2_thru_4 = phrase[1:4] # M[ont]y Python
7. print(letters_2_thru_4)
T his
8. do
cu
m
en = phrase[4:] # Mont[y Python]
9. letter_5_to_end
t be
10. lon
print(letter_5_to_end)
ja No g st gta
u oN pn
11. i na I
uth lk an LKA
12. riz t@re NT#
last_3_letters = ophrase[-3:] Monty Pyt[hon]
ed
c dif ESH
13. print(last_3_letters) opi f .c WA
es om R
all JA
14. ow GT
ed AP
15. !
first_3_letters = phrase[:3] # [Mon]ty Python .
16. print(first_3_letters)
17.
18. three_letters_before_last = phrase[-4:-1] # Monty Py[tho]n
19. print(three_letters_before_last)
Th
20. is
do
cu
21. me = phrase[:] # [Monty Python]
copy_of_string
n tb
22. elo
print(copy_of_string)
n
No agta gs to
j
un p NI
au nilka L
tho nt@ KAN
riz re TE
Code Explanation ed SH
co diff.c
pie om WAR
sa JA
llo GT
The expected output for each print statement we
d! is shownAin
P. square brackets in the comment. Running the file
should result in:
Monty
ont
Th
is
y Python do
cu
hon
m en
tb
elo
Mon n
No agta gs to
j
tho un pn NI
au il LK
tho kant
Monty Python riz @ ANT
ed re ES
co diff.c HW
pie o AR
sa m JA
llo GT
we AP
d! .
T10
his
to 20 minutes
do
cu
me
nt
be
In this exercise, you will write lona program that gets a substring (or slice) from a phrase entered by the user.
N ja g gs
1. Open strings/Exercises/slicing.py.
B. Tells the user what phrase they entered (e.g., Your phrase is 'Hello, World!').
C. Prompts
Th the user for a start number.
is
do
cu
D. Prompts the me user for a end number.
nt
be
lon
E. Tells theNuser jathe gs
ou g tapsubstringto (within single quotes) that starts with the start number and ends with the end
na n i NI
number. uth lk a L K
ori nt@ ANT
ze re d ES
dc
3. Here is the output of the program: op iff.co HWA
ies m R
all JA
ow GT
ed AP
! .
Choose a phrase: Hello, world!
Your phrase is 'Hello, world!'
Character to start with? [Enter number] 4
Character to end with? [Enter number] 9
Your Tsubstring is 'o, wor'
his
do
cu
men
tb
elo
n
No agta gs to
j
u na p n N
Challenge uth ilkan ILKA
ori t N
ze @red TES
dc iff HW
op program.
As with the last exercise, make your ies com respond AR as users would expect.
all JA
ow GT
ed AP
The new program should work like this: ! .
1. def main():
2. phrase = input("Choose a phrase: ")
3. print("Your
T phrase is '", phrase, "'", sep="")
his
4. pos1do= int(input("Character to start with? [Enter number] ")) - 1
cu
en m
5. pos2 = int(input("Character
t to end with? [Enter number] "))
elo b
6. print("Your n
substring is '", phrase[pos1:pos2], "'", sep="")
No jagta gs to
7. p un nil NI
au LK
tho kant
8. main() riz @ ANT
ed re ES
co diff.c HW
pie o AR
sa m JA
llo GT
we AP
d! .
Concatenation and Repetition
Concatenation
Concatenation is a fancy word for stringing strings together. In Python, concatenation is done with the + operator. It
is often usedThtois combine variables with literals as in the following example:
do
cu
me
nt
be
Demo 20: strings/Demos/concatenation.py
lon
No agta gs to
j
un p NI
au nilka LK
1. user_name = input("What tho nt@ is AN your name? ")
riz red TES
ed H + "!"
greeting = "Hello, " +co user_name if
pie f.com WAR
2.
3. print(greeting) s all JA
ow GT
ed AP
! .
Code Explanation
Th
is
1. do
one_knight_says = "nee"
cu
2. me
many_knights_say = one_knight_says * 20
n tb
3. lon
print(many_knights_say)
e
N jag gs
Code Explanation
neeneeneeneeneeneeneeneeneeneeneeneeneeneeneeneeneeneeneenee
Th
is
do
cu
men
tb
elo
n
No jagta gs to
Exercise 13:un Repetition
p
au nilka
NI
L
tho nt@ KAN
riz re TE
ed SH
co diff.c
pie om WAR
sa JA
5 to 10 minutes llo GT
we AP
d! .
Remember our insert_separator() function from the “Hello, You!” programs. It looked like this:
Th
def insert_separator(s="="):
s i
print(s, ds,
oc s, sep="")
u me
nt
be
lon
No agta gs to
j
Using repetition, we can p
un improve insert_separator()
NI so that the number of times the separating character
au nilka L
shows up is passed into the nt@ KAN
thofunction.
riz re TE
ed SH
co diff.c
pie om WAR
1. Open strings/Exercises/hello_you.py sa JAin your editor.
llo GT
we AP
d! .
2. Modify the insert_separator() function so that the number of times the separating character shows up is
passed in as a parameter. It should default to show up 30 times.
3. Modify the calls to insert_separator() so that they pass in an argument to the new parameter.
Th
is
do
cu
Solution: strings/Solutions/hello_you.py
me
nt
be
lon
No jagta gs to
1. def say_hello(name):
un p NI
au nilka LK
t h n A '!', sep='')
ize t@re NTE
2. print('Hello,or', name,
dc d S
3. op iff.co HWA
ies m R
4. def insert_separator(s="=",all repeat=30): JA
ow GT
ed AP
5. print(s * repeat) ! .
6.
7. def recite_poem():
8. print("How about a Monty Python poem?")
9. insert_separator("-", 20)
10. Th
print("Much to his Mum and Dad's dismay")
is
do
cu
11. m
print("Horace ate himself one day.")
en
12. be
print("He didn't
t
stop to say his grace,")
lo n
N jag gs
13. print("He just sat down and ate his face.")
14.
15. def say_goodbye(name):
16. print('Goodbye, ', name, '!', sep='')
17.
18. def main():
19. your_name = input('What is your name? ')
20. insert_separator("-", 20)
Th
is
21. do
say_hello(your_name)
cu
22. me
insert_separator()
n tb
23.
e
recite_poem()lon
No jagta gs to
24. un p
insert_separator() NI
au nilka LK
t
25. h o
say_goodbye(your_name)
riz
n t @ ANT
ed re ES
26. co diff.c HW
pie o AR
sa m JA
27. main() llo GT
we AP
d! .
Th
is
do
Python Stringscu are Immutable
m en
tb
Python strings are immutable, elo meaning that they cannot be changed. However, it is easy to make a copy of a string
N jag ngs
and then assign the ocopy t to
un toapthe sameNI variable as the original string.
au nilka LK
tho n A
riz t@re NTE
e d d SH function, which returns the identity of an object:
op iff.id()
To illustrate, we will use Python’s cbuilt-in WA
ies com R
all JA
ow GT
ed AP
! .
>>> name = 'Nat'
>>> id(name)
1670060967728
>>> name += 'haniel'
>>> name Th
is
'Nathaniel' docu
me
>>> id(name) nt
be
lo
1670060968240 N jag ngs
o tap to
un NI
au nilka L
tho nt@ KAN
riz red TES
ed HW
c
Notice that the id of name changesopwhen iff. we modify
ies com AR the string in the variable.
all JA
ow GT
ed AP
Because strings are immutable, methods that !operate on strings . (i.e., string methods) cannot modify the string in
place. Instead, they return a value. Sometimes that value is a modified version of the string, but it is important to
understand that the original string is unchanged. Consider, for example, the upper() method, which returns a
string in all uppercase letters:
Th
is
do
>>> name = 'Nat'
c u
me
>>> name.upper() # ntReturns uppercase copy of 'Nat'
be
lo
'NAT' N jag ngs
o tap to
un variable NI is unchanged
>>> name # Originalau nilka L
tho nt@ KAN
'Nat' riz r e TE
e dif S dc HW
f. op
ies com AR
all JA
ow GT
ed AP
. the returned value back to the variable:
If you want to change the original variable, you! must assign
>>> 'hELLo'.capitalize()
'Hello'
Th
>>> 'hELLo'.lower()
is
do
'hello' cu
men
>>> 'hELLo'.upper()t be
lon
'HELLO' No jagta gs to
un pn NI
au il LK
tho kant
>>> 'hello world'.title()
riz @ ANT
'Hello World' ed red ES
i H co f
pie f.com WAR
>>> 'hELLo'.swapcase() sa JA
llo GT
'HellO' we AP
d! .
Th
is
do
cu
men
tb
elo
jag ng
N s
Square Brackets in Code Notation
As we mentioned in the Math lesson (see page 59), square brackets in code notation indicate that the contained portion is optional. To illustrate,
consider the str.replace() method: str.replace(old, new[, count])This means that the count parameter, which indicates the maximum
number of replacements to be made, is optional. The syntax allows for nesting optional parameters within optional parameters. For example:
T his that end cannot be specified unless start is also specified. The outside square brackets in [, start[, end]] indicate
This syntax indicates
d
that the whole sectionocisuoptional. The inside square brackets indicate that end is optional even after start is specified. If it were written as
me
[start, end], it would indicatent that start and end are optional, but if one is included, the other must be included as well.
be
lon
No ja gta gs to
un
Methods that Strip Charactersp NI
au nilka L
tho nt@ KAN
riz red TES
ed HW with leading and trailing chars removed.
str.strip([chars]) – Returns c op iff.caostringAR
ies m
all JA
ow GT
str.lstrip([chars]) – Returns aestring d! AP
with leading chars removed.
.
str.isspace()This
do
cu
en m
True if string is made uptofbeonly whitespace.
lon
No ja gta gs to
un p NI
au nilka L
tho nt@ KAN
riz re TE
>>> ' '.isspace() ed SH
co diff.c
True pie om WAR
sa JA
llo GT
>>> ' hi '.isspace() we AP
d! .
False
Th
is
do
'4.2'.isdigit() # False
c um
en
'4.2'.isdecimal() # False
t be
l
o#n False
ja
'4.2'.isnumeric()
gta
No gs
to
na pnilk NIL
u
uth a K
ori nt@ ANT
ze r
d c ediff ESH
op . WA
ies com R
all JA
Beyond that, the difference is mostly academic ow for GTPython developers:
most
ed AP
! .
Th
is
do
So, which should cyou
um use? It doesn’t make much difference really, but isdigit() is the most popular, perhaps
e
because the name mostntclosely
be matches the intention of the function.
lon
N ja g g s
If you’re really curious about it, run strings/Demos/numbers.py, which will create a numbers.txt file in the
same folder showing tabular results, like this (but with much more data):
Th
is
Both of these methods start looking at start index and end looking at end index if start and end are specified.
do
cu
me
nt
be
lon
No agta gs to
j
>>> 'hello'.startswith('h')
un p NI
au nilka L
True tho nt@ KAN
riz re TE
e SH
>>> 'hello'.endswith('o')d co diff.c
pie om WAR
True sa JA
llo GT
we AP
d! .
is… Methods
All the is… methods shown in the table above return False for empty strings, meaning strings with zero length.
Th
sd i
String Methods
oc that Return a Number
um
en
tb
String Methods that Return elo a Position (Index) of a Substring
n
No agta gs to
j
un pn
au ilka NILK
t
str.find(sub[, start[, h ori nt@ end]]) AN
ze r e TE – Returns the lowest index where sub is found. Returns -1 if sub isn’t
found. dc d if SH
op f. WA
ies com R
all JA
str.rfind(sub[, start[, end]]) ow – GT the highest index where sub is found. Returns -1 if sub
Returns
ed AP
! .
isn’t found.
str.index(sub[, start[, end]]) – Same as find(), but errors when sub is not found.
str.rindex(sub[, start[, end]]) – Same as rfind(), but errors when sub is not found.
Th
is
do
cu
me
nt looking at start index and end looking at end index if start and end are specified.
All of these methods start
be
lon
N ja g gs
>>> 'Hello World!'.find('l')
2
>>> 'Hello World!'.rfind('l')
9
>>> 'Hello World!'.index('l')
2
>>> 'Hello
T World!'.rindex('l')
his
9 do
cu
men
tb
elo
n
No jagta gs to
un p NI
au nilka L
str.count(sub[, start[, end]])
tho nt@ KAN
riz red TES
ed HWlooking at start index and end looking at end index.
Returns the number of times subcis op iff.coStart
found. AR
ies m
all JA
ow GT
ed AP
! .
>>>"Hello World!".count('l')
3
Th
is
String Formatting
do
c um
en
Python includes powerfult boptions
elo for formatting strings.
j n g
No agta st
oN
un p
au nilka ILK
The format() Method tho n A
riz t@re NTE
ed d SH
c if
One common way to format stringsopisie to f.use co the
m
WA
format()
R method combined with the Format Specification Mini-
20 sa JA
Language . llo G
we TA
d! P.
Let’s start with a simple example and then we’ll explain the mini-language in detail:
These examples really just show another form of concatenation and could be rewritten like this:
Th
# or is
do
cu
men
tb
elo
movie = 'Monty Python' n jag gs
ou tap N to
ilka NILK
adjective = 'awesome'
n n au
t ho n A ' movie!'
movie + ' is an ' + adjective riz t@re+ N TE
ed
co d iff. SHW
pie com AR
sa JA
llo GT
w
When doing a lot of concatenation, using theedformat() Amethod can be cleaner. However, as the name implies, the
! P.
format() method does more than just concatenation; it also can be used to format the replacement strings. It is
mostly used for formatting numbers.
Format Specification
Th
is
The format specification is separated from the field name or position by a a colon (:), like this:
do
cu
me
nt
be
lon
No agta gs to
{field_name:format_spec}
j
un p NI
au nilka L
tho nt@ KAN
riz re TE
ed SH
co diff.c WA
Because the field name is often leftpout, ies it oismcommonly
R written like this:
all JA
ow GT
ed AP
! .
{:format_spec}
[[fill]align][sign][width][,][.precision][type]
The default formatting for strings and integers are string format (s) and decimal integer (d), which are generally
what you will want, so you can leave the one-letter type specifier off. Consider the following:
Th
is
Demo 23: strings/Demos/formatting_types.py
do
cu
me
nt
be
lon
j gta gs to
1. # Full formatting a strings.
No
2. sentence = 'On un a pscale
n il Nof
ILK {0:d} to {1:d}, I give {2:s} a {3:d}.'
au
tho kant
3. riz
sentence = sentence.format(1, @ ANT 5, 'Monty Python', 6)
ed re ES
co diff.c HW
4. print(sentence) pie o AR
sa m JA
5. llo GT
we AP
d! .
6. # Simplify by removing field names (indexes).
7. sentence = 'On a scale of {:d} to {:d}, I give {:s} a {:d}.'
8. sentence = sentence.format(1, 5, 'Monty Python', 6)
9. print(sentence)
10.
Th
11. is
# Furtherd simplify by removing default type specifiers.
oc
u
12. sentence = m en a scale of {:} to {:}, I give {:} a {:}.'
'On
tb
13. elo
sentence = sentence.format(1, 5, 'Monty Python', 6)
jag n gs
N
ou tap to
ilka NILK
14. print(sentence)
n n
au
tho n A
15. riz t@re NTE
ed d SH
16. # And with the field cname op ifand
f. WA specifier gone, we can
type
ies com R
17. # remove the colon separator. all JA
ow GT
ed AP
18. sentence = 'On a scale of {} to ! {}, I give . {} a {}.'
19. sentence = sentence.format(1, 5, 'Monty Python', 6)
20. print(sentence)
Code Explanation
Th
is
do
cu
me
The final line of code has nt the advantage of being brief, but the disadvantage of being obscure. As a rule, we prefer
be
lon
clarity over brevity.NWe can jag make g it clearer using field names:
ou tap s to
na n N
uth ilkan ILKA
ori t N
ze @red TES
dc iff. HW
>>> 'On a scale of {low} toop{high},
ies com {movie}
AR is a {rating}.'.for ↵↵
mat(low=1, high=5, movie='Monty lloPython', a J A
we GT
rating=6)
d! AP
'On a scale of 1 to 5, Monty Python is a 6.' .
Floats
Th format floats as fixed point numbers using f as the one-letter specifier, which has a default
You will typically
is
precision of 6. If dneither
oc
um type nor precision is specified, floats will be as precise as they need to be to accurately
represent the value. ent b
elo
ja ng
N g s
Fixed point type specified:
Th
is
d
No type specified:ocum
en
tb
elo
n
No jagta gs to
un p NI
>>> import math au nilka L
tho nt@ KAN
r ize T
d c rediff ESH
>>> 'pi equals {}'.format(math.pi)
op . WA
'pi equals 3.141592653589793' ies com R
all JA
ow GT
ed AP
! .
Another formatting option for floats is percentage (%). We will cover that shortly.
Precision
Th
is
do
[[fill]align][sign][width][,][.precision][type]
cu
m en
tb
elo
ja ng
N
The precision is specified
ou g tap s tothe type and is preceded by a decimal point, like this:
before
na n N
uth ilkan ILKA
ori t N
ze @red TES
dc iff HW
op .
>>> import math ies com AR
all JA
o we GT
>>> 'pi equals {:.2f}'.format(math.pi)
d! AP
.
'pi equals 3.14'
Width
Th
is
do
cu
me
[[fill]align][sign][width][,][.precision][type]
n tb
elo
jag ng
N s
The width is an integer indicating the minimum number of characters of the resulting string. If the passed-in string
has fewer characters than the specified width, padding will be added. By default, padding is added after strings and
before numbers, so that strings are aligned to the left and numbers are aligned to the right.
Th
>>> '{:5}'.format('abc')
is
do
cu
'abc ' men
be
>>> '{:5}'.format(123)
t
lo n
' 123' No jagta gs to
un p NI
au nilka L
>>> '{:5.2f}'.format(123) tho nt@ KAN
riz re TE
ed SH
'123.00' co diff.c
pie om WAR
sa JA
llo GT
we AP
d! .
In all three cases, the width of the formatted string is set to 5. Notice the padding on the first two examples: after
the string and before the number.
In the final example, we format the number 123, but the format type has been specified as fixed point (f) with a
precision of 2. So, the resulting string ('123.00') is six characters long – longer than the specified width, so it just
T
returns the fullhisstring without padding.
do
cu
me
nt
Sign be
lon
No agta gs to
j
un p NI
au nilka LK
t h ori n t@ ANT
[[fill]align][sign][width][,][.precision][type]
ze
d c rediff ESH
op . WA
ies com R
all JA
ow GT
By default, negative numbers are preceded eby d! a negativeAPsign,
.
but positive numbers are not preceded by a positive
sign. To force the sign to show up, add a plus sign (+) before the precision, like this:
Alignment
Th
is
do
Options cu
Meaning
me
nt
be
lon
N ja g gs
Options Meaning
Some examples:
>>> '{:>5}'.format('abc')
Th
is
' abc' do
cu
men
tb
lon
>>> '{:<5}'.format(123)
e
No agta gs to
j
'123 ' un p NI
au nilka L
tho nt@ KAN
riz red TES
ed
>>> '{:^5}'.format(123) c op iff.co HWA
ies m R
' 123 ' all JA
ow GT
ed AP
! .
>>> '{:=+5}'.format(123)
'+ 123'
Th
is
Fill do
cu
men
tb
elo
n
No agta gs to
j
[[fill]align][sign][width][,][.precision][type]
un pn NI
au il LK
tho kant
riz @ ANT
ed re ES
By default, spaces are used for padding, co diff.cbut this
HW
pie o Acan be changed by inserting a fill character before the alignment
sa m R
J AG
option, like this (note the period after thellcolon):
ow
ed TA
! P.
>>> '{:.^10.2f}'.format(math.pi)
'...3.14...'
Th
is
do
And now with a dash:cu
me
nt
be
lo
N jag ngs
>>> '{:-^10.2f}'.format(math.pi)
'---3.14---'
Percentage Type
As mentioned
Thearlier, another option for type is percentage (%):
is
do
cu
me
nt
be
>>> questions = 25 lon
No ja gta gs to
>>> correct = 18 un p NI
au nilka L
>>> grade = correct /thoquestions nt@ KAN
riz re TE
ed SH
co diff.c
om WAR
>>> grade pie
sa JA
0.72 llo GT
we AP
>>> '{:.2f}'.format(grade) d! .
'0.72'
>>> '{:.2%}'.format(grade)
'72.00%'
>>> '{:.0%}'.format(grade)
Th
'72%' is
do
cu
men
tb
elo
n
No agta gs to
j
un pn NI
Long Lines of Code au il LK
tho kant
riz @ ANT
ed red ES
The Python Style Guide 23 suggests co that iff. linesH of code should be limited to 79 characters. This can be difficult as
pie c om WAR
s
each line of code is considered a new statement;all however,
JA it can usually be accomplished through some
ow GT
combination of: ed AP
! .
2. Concatenation.
3. Th
Triple-quoted multi-line strings.
is
do
cu
me
nt
be
lo
All three methods are jag ningsthe following file:
No shown tap to
un NI
au nilka L
tho nt@ KAN
r i
Demo 24: strings/Demos/long_code_lines.pyz r e TE
ed SH
co diff.c
pie om WAR
sa JA
llo GT
# EXAMPLE 1: Breaking method earguments
w A
1. d! P.
across multiple lines
2. phrase = ("On a scale of {} to {}, I give {} a {}.".format(1, 5,
3. "Monty Python", 6))
4. print(phrase)
5.
6. Th
location = "ponds"
is
oc d
7. items = "swords"
u me
nt
8. be
beings = "masses"
lon
9. jag gs
adjective N= "farcical"
10.
11. # EXAMPLE 2: Concatenation
12. quote = ("Listen, strange women lyin' in {} " +
13. "distributin' {} is no basis for a system of " +
14. "government. Supreme executive power derives from " +
15. "a mandate from the {}, not from some {} " +
16. "aquatic ceremony.").format(location, items,
Th
is
17. do beings, adjective)
cu
18. men
tb
19. print(quote)
elo
n
No jagta gs to
20. un p NI
au nilka LK
t
21. # EXAMPLE 3: Tripleh o n t
riz quotes@ ANT
ed re ES
co diffwomen H
22. quote = """Listen, strange pie .com Wlyin'
AR in {} \
sa J AG of \
23. distributin' {} is no basis llofor a system
we TA
24. government. Supreme executive power d ! P. from \
derives
25. a mandate from the {}, not from some {} \
26. aquatic ceremony.""".format(location, items,
27. beings, adjective)
28.
T
his
29. print(quote)
do
cu
men
tb
elo
Code Explanation N jag ngs
ou ta to
na pnilk NIL
uth an K
ori t@ ANT
Remember that the backwards zeslashes
r ES end of each line escape the newline character.
d c edifat f.c
the
HW
op
ies om AR
all JA
Also, notice that the arguments passed to w o format() GT broken across lines and vertically aligned to make it clear
are
ed AP
! .
that they are related.
Th
Exercise
is
do 14: Playing with Formatting
cu
men
tb
elo
n
No agta gs to
j
u
10 to 20 minutes
na p n N
uth ilkan ILKA
ori t N
ze @red TES
dc iff. HW
op
ies com AR
In this exercise, you will practice formatting all strings. Here
JA are two options for practicing:
ow GT
ed AP
! .
Option 1
2. Try formatting different values in different ways. For example, try running:
Th
is
do
cu
me
nt
>>> '{:.0%}'.format(.87)be
lon
N ja g gs
Option 2
Th
is
do
cu
men
tb
elo
Formatted String ng
No agLiterals
j
t st (f-strings)
un apni o NI
au lka 24LK
Formatted string literals or thf-strings
ori nt@ use AN a syntax that implicitly embeds the format() function within the string
ze re TE
itself. The coding tends to be less d
d cverbose. S
op iff.co HWA
ies m R
all JA
ow GT
e
The following demonstration compares stringdconcatenation AP and string formatting with the f-string syntax.
! .
1. import math
2. Th
user_name = input("What is your name? ")
is
do
cu
3. men
4. # Concatenation: be t
lon
ja g gs
5. greeting =N "Hello, " + user_name + "!"
6.
7. # The format() method:
8. greeting = "Hello, {}!".format(user_name)
9.
10. # f-string:
11. greeting = f"Hello, {user_name}!"
12. print(greeting)
Th
is
13. do
cu
14. me
# format specification is also available:
n tb
15. pi_statement = f"pi
e
lon is {math.pi:.4f}"
No jagta gs to
un
16. print(pi_statement) p NI
au nilka L
tho nt@ KAN
riz re TE
ed SH
co diff.c
pie om WAR
Code Explanation sa JA
llo GT
we AP
d! .
The curly braces within the f-string contain the variable name and optionally a format specification. The string literal
is prepended with an f.
Everything you learned earlier about formatting can be applied to the f-string because the same formatting function
is called. Practice
Th with f-strings by running the following lines of code:
is
do
cu
me
nt
be
>>> import math lon
No agta gs to
j
>>> movie = 'Monty un Python'p NI
au nilka LK
>>> adjective = 'awesome' t h ori n t@ ANT
ze
d c rediff ESH
>>> f'{movie} is an {adjective} op .
movie!' WA
ies com R
'Monty Python is an awesome movie!' a llo JA
we GT
d AP
>>> low = 1 ! .
>>> high = 5
>>> rating = 6
>>> f'On a scale of {low} to {high}, {movie} is a {rating}.'
'On a scale of 1 to 5, Monty Python is a 6.'
T
his
>>> f'pi equals {math.pi:f}'
do
um c
'pi equals 3.141593'
e nt
be
>>> f'pi equals {math.pi}'
lo ng
No ja
gta s
to
'pi equals 3.141592653589793'
p
una nil N
uth kan ILKA
>>> f'pi equals {math.pi:.2f}'
ori t N
ze @red TES
'pi equals 3.14' dc iff. HW
op
ies com AR
>>> f'{1000000:,.2f}' all JA
ow GT
'1,000,000.00' e d! AP
.
>>> f"{'abc':20}"
'abc '
>>> f'{123:20}'
' 123'
Th
>>> f'{123:5.2f}'
i sd
oc
'123.00' um
en
tb
>>> f'pi equals {math.pi:+.2f}'
e lon
N
'pi equals +3.14' jag gs
>>> f"{'abc':>20}"
' abc'
>>> f'{123:<20}'
'123 '
>>> f'{123:=+20}'
'+ 123'
>>> f'pi equals {math.pi:.^10.2f}'
hs T
'pi equals i...3.14...'
do
cu
>>> f'pi equals m en
{math.pi:-^10.2f}'
tb
'pi equals ---3.14---'lon
e
No jagta gs to
>>> un p NI
au nilka LK
t
>>> questions = 25 h o riz
n t @ ANT
ed re ES
>>> correct = 18 co diff.c HW
pie o AR
sa m JA
>>> grade = correct / questions llo GT
we AP
>>> f'{grade:.2f}' d! .
'0.72'
>>> f'{grade:.2%}'
'72.00%'
his T
>>> f'{grade:.2f}'
do
cu
'0.72' men
tb
elo
n
No agta gs to
j
un pn NI
au il LK
tho kant AN
Built-in String Functions r ize @ T
d c rediff ESH
op . WA
ies com R
str(object) a llo JA
we GT
d! AP
.
Converts object to a string.
Th
>>> len('foo')
is
do
3 cu
men
tb
elo
jag ng
N s
min() and max() 26
The min(args) function returns the smallest value of the passed-in arguments.
The max(args) function returns the largest value of the passed-in arguments.
>>> min('w',
T 'e', 'b')
his
'b' do
cu
m
>>> min('a', 'B', en
'c')
tbe
lon
j gta gs to
'B' a
No
>>> max('w', 'e',un'b') p NI
au nilka L
tho nt@ KAN
'w' riz red TES
ed
>>> max('a', 'B', 'c')
c op iff.co HWA
ies m R
all JA
'c' ow GT
ed AP
! .
Note that all uppercase letters come before lowercase letters (e.g., min('Z', 'a') returns ‘Z’).
In this exercise, you will write a program that repeatedly prompts the user for a Company name, Revenue, and
Expenses and then outputs all the information as tab-delimited text. Here is the program after it has run:
Th
is
do
cu
en m
Company: Pepperpots t
be
lon
No jagta gs to
Revenue: 1200000
Expenses: 999002 una pnilk NIL
uth an KA
riz at@row
Again? Press ENTER to oadd NT Q to quit.
ed red or ES
c o iff . HW
Company: Ni Knights pie c o AR
sa m JA
Revenue: 19 l low GT
ed AP
Expenses: 24 ! .
In this exercise, you can use the format() method or f-strings or any combination of the two.
1. Open strings/Exercises/tab_delimited_text.py.
Th
is
2. Modify the daddheaders()
oc function so that it creates a header row and appends it to _output. The four
um
e
headers should each nt take up 10 spaces, be aligned to the center, and be separated by tabs, like this:
be
lon
No ja gta gs to
un pn NI
' Company \t au il L
tho kant \tKAExpenses
Revenue
N
\t Profit \n'
riz @
ed red TES
co if H
pie f.com WAR
sa JA
Don’t just copy that string. Use the lformat()low GT
method.
ed AP
! .
3. Modify the addrows() function so that it adds a row to _output by prompting the user for values for
company, revenue, and expenses, and then calculating profit.
Th
Solution: strings/Solutions/tab_delimited_text.py
is
do
cu
me
nt
1. _output = "" be
lon
ja g
2. N ou g tap s to
na n N
3. def add_headers(): uth ilkan ILKA
ori t@ NT
z r ES
global _output ed c ediff
.co HWA
4.
op
ies m R
5. c_header = "{:^10}".format("Company")all JA
ow GT
6. r_header = "{:^10}".format("Revenue") ed AP
! .
7. e_header = "{:^10}".format("Expenses")
8. p_header = "{:^10}".format("Profit")
9. _output += "{}\t{}\t{}\t{}\n".format(c_header, r_header,
10. e_header, p_header)
11. Th
is
oc d
12. def add_row():
um
en
13. tb
global _output
e lon
14. No jagta gs to
un p NI
15. c = au nilka
input("Company: LK
tho nt@ ") AN
riz red TES"))
16. r = ed
float(input("Revenue:
HW
co if
17. e = float(input("Expenses: pie f.com ")) AR
sa JA
llo GT
18. p = r - e # profit we AP
d! .
19.
20. c_str = "{:<10}".format(c)
21. r_str = "${:>10,.2f}".format(r)
22. e_str = "${:>10,.2f}".format(e)
23. p_str
T = "${:>10,.2f}".format(p)
his
24. do
cu
m
25. new_row e=nt"{}\t{}\t{}\t{}\n".format(c_str, r_str, e_str, p_str)
be
lon
26.
No jagta gs to
_output u+= pn
na new_row N
27.
uth ilkan ILKA
ori t N
-------Lines 28 through ze @r39 T
Omitted-------
d c ediff ESH
op .co WA
ies m R
all JA
ow GT
ed AP
! .
Solution: strings/Solutions/tab_delimited_text_f_string.py
1. _output = ""
2.
3.
T
his
def add_headers():
do
cu _output
4. global m en
t
5. c_header = bf"{'Company':^10}"
elo
ja ng
6. N
r_header s
= gf"{'Revenue':^10}"
7. e_header = f"{'Expenses':^10}"
8. p_header = f"{'Profit':^10}"
9. _output += f"{c_header}\t{r_header}\t{e_header}\t{p_header}\n"
10.
11. def add_row():
12. global _output
13.
Th
14. c i=s dinput("Company:
oc ")
um
15. en
r = float(input("Revenue: "))
tb
16.
e
lon
e = float(input("Expenses: "))
No jagta gs to
17. p
p = r - uen # profit NI
au nilka L
18. tho nt@ KAN
riz re TE
ed SH
co diff.c
19. c_str = f"{c:<10}" pie om WAR
sa JA
20. r_str = f"${r:>10,.2f}" llo GT
we AP
21. e_str = f"${e:>10,.2f}" d! .
22. p_str = f"${p:>10,.2f}"
23.
24. new_row = f"{c_str}\t{r_str}\t{e_str}\t{p_str}\n"
25.
This
26. _output += new_row
do
c
um 27 through 38 Omitted-------
-------Lines e nt
be
lon
No agta gs to
j
un p NI
au nilka L
tho nt@ KAN
Conclusion r ize T
d c rediff ESH
op . WA
ies com
In this lesson, you have learned to manipulateall andRformat
JA strings.
ow GT
ed AP
! .
17. https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str
Th
is
do is no “character” type in Python. A single character is just a string of length 1. So, when you
18. Note that therecu
me
index a string, you ntget multiple one-character strings (or strings of length 1).
be
lon
No jagta gs to
un p NI
au nilka L
tho nt@ KAN
19. Three single quotes willrizworked as
r e well,TEbut double quotes are recommended. More information at
SH
co diff.c W
pie o AR
sa m
https://www.python.org/dev/peps/pep-0008/#string-quotes. JA
llo GT
we AP
d! .
20. https://docs.python.org/3/library/string.html#formatspec
brary/string.html#formatspec.
23. https://www.python.org/dev/peps/pep-0008/#maximum-line-length
Th
is
do
cu
me
nt
24. f-strings were introduced in Python 3.6.
be
lon
No ja gt g st
un apni o NI
au lka L
tho nt@ KAN
r
25. As we will see later, the ilen() ze T
d c refunction
dif ESHcan also take objects other than strings.
f
op . WA
ies com R
all JA
ow GT
ed AP
! .
26. The min() and max() functions can also compare numbers.
Th
is
do
cu
men
tb
elo
n
No agta gs to
j
un pn NI
au il LK
tho kant
riz @ ANT
ed re ES
co diff.c HW
pie o AR
sa m JA
llo GT
we AP
d! .
Th
is
do
cu
men
tb
elo
n
No agta gs to
j
un pn NI
au il LK
tho kant
riz @ ANT
ed re ES
co diff.c HW
pie o AR
sa m JA
llo GT
we AP
d! .
Th
is
do
cu
men
tb
elo
jag ng
N s