Complete the code to replace all occurrences of 'cat' with 'dog' in the text.
text = 'The cat sat on the cat mat.' new_text = text.[1]('cat', 'dog') print(new_text)
The replace method replaces all occurrences of a substring with another substring.
Complete the code to replace only the first occurrence of 'apple' with 'orange'.
text = 'apple banana apple grape' new_text = text.[1]('apple', 'orange', 1) print(new_text)
The replace method can take a third argument to limit how many replacements happen.
Fix the error in the code to replace 'blue' with 'red' in the string.
text = 'blue sky' new_text = text.[1]('blue', 'red') print(new_text)
The correct method name is replace all lowercase. Python is case-sensitive.
Fill both blanks to create a dictionary with words as keys and their lengths as values, but only for words longer than 3 letters.
words = ['apple', 'bat', 'carrot', 'dog'] lengths = {word: [1] for word in words if len(word) [2] 3} print(lengths)
Use len(word) to get the length and filter words with length greater than 3 using >.
Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, only for words longer than 4 letters.
words = ['tree', 'house', 'car', 'elephant'] result = { [1]: [2] for w in words if len(w) [3] 4 } print(result)
Use w.upper() for uppercase keys, len(w) for values, and filter words longer than 4 with >.