[Python] String Join

Let's introduce a new string method, join:

>>> nautical_directions = "
".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
原文地址:https://www.cnblogs.com/Answer1215/p/7873108.html