Chapter Review Questions#
What is printed by this fragment? Predict first, then run the cell to check:
>>> s = "question" >>> print(len(s)) 8 >>> print(s[2]) e >>> print(s[2:5]) est >>> print(s[3:]) stion >>> print(s.find("ti")) 4 >>> print(s.find("to")) -1
What is printed by this fragment? Predict first, then run the cell to check:
>>> s = "Word" >>> s.upper() 'WORD' >>> print(s) Word
What is printed by this fragment? Predict first, then run the cell to check:
>>> a = "hi" >>> b = a.upper() >>> print(a + b) hiHI
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? Predict first, then run the cell to check:
>>> parts = "2024-05-01".split("-") >>> print(parts) ['2024', '05', '01'] >>> print("-".join(parts)) 2024-05-01
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().