Let's introduce a new string method, join
:
>>> nautical_directions = "\n".join(["fore", "aft", "starboard", "port"])
>>> print(nautical_directions)
fore
aft
starboard
port
Also note thatjoin
will trigger an error if we try to join anything other than strings. For example:
>>> stuff = ["thing", 42, "nope"]
>>> " and ".join(stuff)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 1: expected str instance, int found