WEB PRACTICE CHAPTER 04

Working with Strings

Ten additional coding tasks · W031–W040

These extra website exercises are not a substitute for the book's exact numbered questions. Follow your printed book separately using the 100-challenge tracker.
Chapter 4 code

Choose starters to try yourself, or download a separate worked example pack after attempting the tasks.

Chapter 4 starters ↓Chapter 4 worked examples ↓
WEB PRACTICE W031

Count vowels

Count the vowels in an example sentence.

Reveal a hint

Make the text lowercase first.

Reveal example code (after trying it yourself)
text = "Programming is creative"
vowels = sum(1 for c in text.lower() if c in "aeiou")
print("Vowels:", vowels)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W032

Reverse a name

Reverse the letters of the word Python.

Reveal a hint

Try slicing with a step of -1.

Reveal example code (after trying it yourself)
word = "Python"
print(word[::-1])

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W033

Initials generator

Create initials for the name Ada Lovelace.

Reveal a hint

Split the name at spaces.

Reveal example code (after trying it yourself)
name = "Ada Lovelace"
initials = "".join(part[0].upper() for part in name.split())
print(initials)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W034

Trim spaces

Clean extra spaces around a typed answer and lower-case it.

Reveal a hint

Use strip() and lower() together.

Reveal example code (after trying it yourself)
answer = "  YeS  "
cleaned = answer.strip().lower()
print(cleaned)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W035

Simple palindrome

Check whether racecar reads the same backwards.

Reveal a hint

Compare the string with its reverse.

Reveal example code (after trying it yourself)
word = "racecar"
print("Palindrome" if word == word[::-1] else "Not palindrome")

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W036

Count words

Count the words in an example sentence.

Reveal a hint

split() creates a list of words.

Reveal example code (after trying it yourself)
sentence = "Python helps me solve problems"
print("Word count:", len(sentence.split()))

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W037

Replace a word

Replace a placeholder in a sentence with the word Python.

Reveal a hint

Use string replace().

Reveal example code (after trying it yourself)
message = "I enjoy SUBJECT lessons."
print(message.replace("SUBJECT", "Python"))

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W038

Mask a code

Show the final four characters of a sample reference code.

Reveal a hint

Slicing with [-4:] takes the last four.

Reveal example code (after trying it yourself)
reference = "ABCDEF4826"
print("Reference ending:", reference[-4:])

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W039

Count letters

Count the number of letter a characters, ignoring case.

Reveal a hint

Use lower() and count().

Reveal example code (after trying it yourself)
text = "Amazing Adventures"
print("a letters:", text.lower().count("a"))

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W040

Alphabetic characters

Extract alphabetic characters from a mixed string.

Reveal a hint

Use isalpha() in a loop.

Reveal example code (after trying it yourself)
text = "Py3thon! 2026"
letters = "".join(c for c in text if c.isalpha())
print(letters)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.