Practice: String Methods

« Read the chapter

Q1. Shout it

BeginnerCoding

Print the text "quiet please" in capital letters.

Goal: Change case.

Q2. Trim the ends

BeginnerCoding

Remove the spaces around " hi " and print it between square brackets.

Goal: Trim text.

Q3. Swap a word

BeginnerCoding

Replace cats with dogs in "I like cats and cats" and print the result.

Goal: Replace text.

Q4. Count letters

BeginnerCoding

Print how many times the letter s appears in "mississippi".

Goal: Count substrings.

Q5. Words to list

IntermediateCoding

Split "one two three" into words and print the list.

Goal: Split text.

Q6. Join with dashes

IntermediateCoding

Join the list ["a", "b", "c"] with a dash and print the result.

Goal: Join text.

Q7. Title case name

IntermediateCoding

Print "mary ann smith" with each word starting with a capital letter.

Goal: Use title.

Q8. First vowel position

IntermediateCoding

Print the index of the first letter e in "scheme", and print -1 style result for letter z in the same line, separated by a space.

Goal: Use find.

Q9. Clean CSV line

AdvancedCoding

Split "x, y ,z" at commas, strip each part, and print the cleaned list.

Goal: Combine methods.

Q10. Case methods

BeginnerOutput

What does this print?

Code
s = "Python"
print(s.lower(), s.upper())

Goal: Predict case methods.

Q11. Original unchanged

BeginnerOutput

What does this print?

Code
s = "abc"
s.upper()
print(s)

Goal: Know strings are immutable.

Q12. Find results

IntermediateOutput

What does this print?

Code
s = "banana"
print(s.find("an"), s.find("an", 2), s.rfind("an"))

Goal: Predict searches.

Q13. Strip characters

IntermediateOutput

What does this print?

Code
print("xxhixx".strip("x"), "abcba".strip("ab"))

Goal: Understand strip arguments.

Q14. Split limit

IntermediateOutput

What does this print?

Code
print("a-b-c-d".split("-", 2))

Goal: Use maxsplit.

Q15. Partition

AdvancedOutput

What does this print?

Code
print("k=v=w".partition("="))
print("abc".partition("="))

Goal: Know partition.

Q16. Forgot to assign

BeginnerDebugging

This code should print HELLO but prints hello. Fix it.

Code
word = "hello"
word.upper()
print(word)

Goal: Fix immutability errors.

Q17. Join numbers

IntermediateDebugging

This code raises a TypeError. Fix it so it prints 1,2,3.

Code
nums = [1, 2, 3]
print(",".join(nums))

Goal: Convert before join.

Q18. Wrong find test

IntermediateDebugging

This code says the word is missing even though it starts the text. Fix it to print found.

Code
s = "apple pie"
if s.find("apple"):
    print("found")
else:
    print("missing")

Goal: Handle find results.

Q19. find vs index

IntermediateConceptual

Explain when you would pick index instead of find.

Goal: Choose the right search method.

Q20. Why return new strings

BeginnerConceptual

Explain why s.replace('a','b') does not change s.

Goal: Explain immutability.