Practice: String Methods
Q1. Shout it
Print the text "quiet please" in capital letters.
Goal: Change case.
Q2. Trim the ends
Remove the spaces around " hi " and print it between square brackets.
Goal: Trim text.
Q3. Swap a word
Replace cats with dogs in "I like cats and cats" and print the result.
Goal: Replace text.
Q4. Count letters
Print how many times the letter s appears in "mississippi".
Goal: Count substrings.
Q5. Words to list
Split "one two three" into words and print the list.
Goal: Split text.
Q6. Join with dashes
Join the list ["a", "b", "c"] with a dash and print the result.
Goal: Join text.
Q7. Title case name
Print "mary ann smith" with each word starting with a capital letter.
Goal: Use title.
Q8. First vowel position
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
Split "x, y ,z" at commas, strip each part, and print the cleaned list.
Goal: Combine methods.
Q10. Case methods
What does this print?
s = "Python"
print(s.lower(), s.upper())Goal: Predict case methods.
Q11. Original unchanged
What does this print?
s = "abc"
s.upper()
print(s)Goal: Know strings are immutable.
Q12. Find results
What does this print?
s = "banana"
print(s.find("an"), s.find("an", 2), s.rfind("an"))Goal: Predict searches.
Q13. Strip characters
What does this print?
print("xxhixx".strip("x"), "abcba".strip("ab"))Goal: Understand strip arguments.
Q14. Split limit
What does this print?
print("a-b-c-d".split("-", 2))Goal: Use maxsplit.
Q15. Partition
What does this print?
print("k=v=w".partition("="))
print("abc".partition("="))Goal: Know partition.
Q16. Forgot to assign
This code should print HELLO but prints hello. Fix it.
word = "hello"
word.upper()
print(word)Goal: Fix immutability errors.
Q17. Join numbers
This code raises a TypeError. Fix it so it prints 1,2,3.
nums = [1, 2, 3]
print(",".join(nums))Goal: Convert before join.
Q18. Wrong find test
This code says the word is missing even though it starts the text. Fix it to print found.
s = "apple pie"
if s.find("apple"):
print("found")
else:
print("missing")Goal: Handle find results.
Q19. find vs index
Explain when you would pick index instead of find.
Goal: Choose the right search method.
Q20. Why return new strings
Explain why s.replace('a','b') does not change s.
Goal: Explain immutability.