[Python] String Join

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

 

posted @ 2017-11-21 14:45  Zhentiw  阅读(495)  评论(0编辑  收藏  举报