原文链接:点我
Overview
This post will show some examples of the Python join method. What is important to remember is that the character that joins the elements is the one upon which the function is called.
Join Examples
Let's show an example
Creating a new list
>>> music = ["Abba","Rolling Stones","Black Sabbath","Metallica"]
>>> print music
['Abba', 'Rolling Stones', 'Black Sabbath', 'Metallica']
Join a list with an empty space
>>> print ' '.join(music)
Abba Rolling Stones Black Sabbath Metallica
Join a list with a new line
>>> print "\n".join(music)
Abba
Rolling Stones
Black Sabbath
Metallica
Join a list with a tab
>>> print "\t".join(music)
Abba Rolling Stones Black Sabbath Metallica
>>>
Happy scripting!

本文通过具体实例展示了 Python 中 join 方法的使用方式。介绍了如何利用不同的字符(如空格、换行符、制表符等)将列表中的字符串元素连接起来。
247

被折叠的 条评论
为什么被折叠?



