Exercises
Modify Strings
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Remove the whitespace from both ends and print the result with repr so you can see it worked.
Python
name = " Ada Lovelace "
# print the trimmed name using repr
strip() removes whitespace from both ends.
name = " Ada Lovelace "
print(repr(name.strip()))Exercise 2Passed
Remove the www. prefix safely, without chewing off other letters.
Python
host = "www.example.com"
# print example.com
strip() would eat too much. There is a method for an exact prefix.
host = "www.example.com"
print(host.removeprefix("www."))Exercise 3Passed
Split the line on commas and join the pieces back together with and between them.
Python
line = "one,two,three"
# print: one and two and three
The separator is the string you call join on.
line = "one,two,three"
print(" and ".join(line.split(",")))