[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

 

    原文作者:Zhentiw
    原文地址: https://www.cnblogs.com/Answer1215/p/7873108.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞