Exercises
String Methods
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Print True for each name ending in .png or .jpg, without a chain of or.
Python
for name in ["a.png", "b.txt", "c.jpg"]:
# print the name and whether it is an image
passendswith accepts a tuple of endings.
for name in ["a.png", "b.txt", "c.jpg"]:
print(name, name.endswith((".png", ".jpg")))Exercise 2Passed
Print where two appears in the line, and how many times one appears.
Python
line = "one,two,one"
# print the index of two, then the count of one
find() gives an index, count() tallies.
line = "one,two,one"
print(line.find("two"))
print(line.count("one"))Exercise 3Passed
Split the setting on the first = only, into three parts, and print them.
Python
setting = "key=value=more"
# print the three parts of a partition
partition always gives exactly three pieces.
setting = "key=value=more"
print(setting.partition("="))