Chapter Review Questions#
Note
Source: Questions 1–5 adapted from the C# edition
(basicstringops/reviewstring.rst). Questions 6–10 are original
additions specific to Python.
What is printed by this fragment?
s = "question" print(len(s)) print(s[2]) print(s[2:5]) print(s[3:]) print(s.find("ti")) print(s.find("to"))
What is printed by this fragment?
s = "Word" s.upper() print(s)
What is printed by this fragment?
a = "hi" b = a.upper() print(a + b)
Strings and mutability.
Are strings mutable or immutable?
What does that mean in practice when you call a method like
s.upper()?
Both
s.find(sub)ands.index(sub)search for a substring. What is the difference in their behavior when the substring is not present ins?Write an expression that extracts the last three characters of a string
s, without using a literal index number.What does
s[::-1]do?Write a function
count_vowels(s)that returns the number of vowel characters (a,e,i,o,u, case-insensitive) in strings.What is printed by this fragment?
parts = "2024-05-01".split("-") print(parts) print("-".join(parts))
Write a function
title_case(s)that returnssconverted to title case (first letter of each word capitalized) without using the built-instr.title()method. Usesplit()andjoin().