Home//

[Solved] Python for Everybody - Chapter 6: Strings

[Solved] Python for Everybody - Chapter 6: Strings

Minh Vu

By Minh Vu

Updated Nov 28, 2023

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:

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:

exercise-1.py
fruit = "banana" index = len(fruit) - 1 while index >= 0: print(fruit[index]) index -= 1

Output:

output
a n a n a b

Explanation:

  1. Create a fruit variable and set it to anything you want, here I set it to "banana".
  2. Create an index variable and set it to len(fruit) - 1 as we want to iterate from the last character to the first character.
  3. Create a while loop with the condition index >= 0.
  4. Inside the loop, print the character at the index position.
  5. Decrement the index by 1 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 to 0 if not specified),
  • to the end index exclusively (default to len(fruit) if not specified),
  • with the step value (default to 1 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.

exercise-3.py
word = "banana" count = 0 for letter in word: if letter == "a": count += 1 print(count)

Answer:

exercise-3.py
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:

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.

  1. Create a count function with 2 arguments: string and letter.
  2. Create a count_char variable and set it to 0.
  3. Create a for loop to iterate through each character in the string.
  4. Inside the loop, check if the char is equal to the letter, if yes, increment the count_char by 1.
  5. 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:

exercise-4.py
print("banana".count("a"))

Output:

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 to 0.
  • end: the ending index of the search, default to len(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:

exercise-5.py
str = "X-DSPAM-Confidence:0.8475" colon_index = str.find(":") number = float(str[colon_index + 1:]) print(number)

Output:

output
0.8475

Explanation:

  1. Create a str variable and set it to "X-DSPAM-Confidence:0.8475" as the problem requires.
  2. Create a colon_index variable and set it to str.find(":") to get the index of the : character.
  3. Create a number variable and set it to float(str[colon_index + 1:]) to get the substring after the : character and convert it to a floating point number.
  4. 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:

exercise-6.py
str = " Hello, World! " print(str.strip()) print(str.replace("World", "Minh Vu"))

Output:

output
Hello, World! Hello, Minh Vu!

Explanation:

  1. Create a str variable and set it to " Hello, World! " as the problem requires.
  2. Print the str variable after stripping (removing all leading and trailing whitespaces) with the strip method.
  3. Print the str variable after replacing all occurrences of "World" with "Minh Vu" with the replace 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.

You can search for other posts at home page.
Minh Vu

Minh Vu

Software Engineer

Hi guys, I'm the author of WiseCode Blog. I mainly work with the Elastic Stack and build AI & Python projects. I also love writing technical articles, hope you guys have good experience reading my blog!