This tutorial gives you the solutions to the exercises in Chapter 6 of the book Python for Everybody: Exploring Data Using Python 3 by Charles R. Severance.
Link to the book:
Solutions for other chapters:
- [Solved] Python for Everybody - Chapter 5: Iteration
- [Solved] Python for Everybody - Chapter 6: Strings (this tutorial)
- [Solved] Python for Everybody - Chapter 8: Lists
Table of Contents
1. Chapter 6: Strings - Exercise 1
Exercise 1: Write a while
loop that starts at the last character in the string and works its way backwards to the first character in the string, printing each letter on a separate line, except backwards.
Answer:
fruit = "banana" index = len(fruit) - 1 while index >= 0: print(fruit[index]) index -= 1
Output:
a n a n a b
Explanation:
- Create a
fruit
variable and set it to anything you want, here I set it to"banana"
. - Create an
index
variable and set it tolen(fruit) - 1
as we want to iterate from the last character to the first character. - Create a
while
loop with the conditionindex >= 0
. - Inside the loop, print the character at the
index
position. - Decrement the
index
by1
after each iteration.
2. Chapter 6: Strings - Exercise 2
Exercise 2: Given that fruit
is a string, what does fruit[:]
mean?
Answer: fruit[:]
means the whole string fruit
, or in other words, it means fruit[:] == fruit
.
Explanation:
This problem relates to the list slicing technique in Python.
In short, you can use the syntax fruit[start:end:step]
to get a sub-string from the fruit
string:
- from the
start
index inclusively (default to0
if not specified), - to the
end
index exclusively (default tolen(fruit)
if not specified), - with the
step
value (default to1
if not specified).
For example, with fruit = "banana"
:
fruit[1:5:2]
will return"at"
.fruit[3:7]
will return"erm"
.fruit[:]
will return"banana"
.
So, fruit[:]
will return the whole string "banana"
. Because the start
and end
are not specified, they will default to 0
and len(fruit)
respectively.
There are some other common syntaxes like
[-1]
,[:n]
,[-n:]
,[::-1]
. You can learn more through this tutorial: Python List [-1], [:n], [-n:], [::-1] Explained.
3. Chapter 6: Strings - Exercise 3
Exercise 3: Encapsulate this code in a function named count
, and generalize it so that it accepts the string and the letter as arguments.
word = "banana" count = 0 for letter in word: if letter == "a": count += 1 print(count)
Answer:
def count(string, letter): count_char = 0 for char in string: if char == letter: count_char += 1 return count_char print(count("banana", "a"))
Output:
3
Explanation:
In this problem, we just wrap the whole code in a function named count
and pass the string
and letter
as arguments.
- Create a
count
function with 2 arguments:string
andletter
. - Create a
count_char
variable and set it to0
. - Create a
for
loop to iterate through each character in thestring
. - Inside the loop, check if the
char
is equal to theletter
, if yes, increment thecount_char
by1
. - Return the
count_char
after the loop.
There are 3 a
in the string "banana"
, so the function will return 3
.
To count the number of occurrences of all characters, you can read this post: How to Count Character Occurrences in String in Python.
4. Chapter 6: Strings - Exercise 4
Exercise 4: There is a string method called count
that is similar to the function in the previous exercise. Read the documentation of this method at:
https://docs.python.org/library/stdtypes.html#string-methods
Write an invocation that counts the number of times the letter a occurs in "banana"
.
Answer:
print("banana".count("a"))
Output:
3
Explanation:
The count
method is a built-in method of the str
class in Python. It returns the number of occurrences of the substring
in the string
.
The general syntax is: string.count(substring, start, end)
substring
: the substring to search for.start
: the starting index of the search, default to0
.end
: the ending index of the search, default tolen(string)
.
5. Chapter 6: Strings - Exercise 5
Exercise 5: Take the following Python code that stores a string:
str = "X-DSPAM-Confidence:0.8475"
Use find
and string slicing to extract the portion of the string after the colon character and then use the float
function to convert the extracted string into a floating point number.
Answer:
str = "X-DSPAM-Confidence:0.8475" colon_index = str.find(":") number = float(str[colon_index + 1:]) print(number)
Output:
0.8475
Explanation:
- Create a
str
variable and set it to"X-DSPAM-Confidence:0.8475"
as the problem requires. - Create a
colon_index
variable and set it tostr.find(":")
to get the index of the:
character. - Create a
number
variable and set it tofloat(str[colon_index + 1:])
to get the substring after the:
character and convert it to a floating point number. - Print the
number
variable.
6. Chapter 6: Strings - Exercise 6
Exercise 6: Read the documentation of the string methods at https://docs.python.org/library/stdtypes.html#string-methods. You might want to experiment with some of them to make sure you understand how they work. strip
and replace
are particularly useful.
The documentation uses a syntax that might be confusing. For example, in find(sub[, start[, end]])
, the brackets indicate optional arguments. So sub
is required, but start
is optional, and if you include start
, then end
is optional.
Answer:
str = " Hello, World! " print(str.strip()) print(str.replace("World", "Minh Vu"))
Output:
Hello, World! Hello, Minh Vu!
Explanation:
- Create a
str
variable and set it to" Hello, World! "
as the problem requires. - Print the
str
variable after stripping (removing all leading and trailing whitespaces) with thestrip
method. - Print the
str
variable after replacing all occurrences of"World"
with"Minh Vu"
with thereplace
method.
Conclusion
In this tutorial, we have solved 6 problems of the Chapter 6: Strings in the book Python for Everybody: Exploring Data Using Python 3.
Please leave a comment if you have any questions.