您的位置:首页 > 编程语言 > Python开发

Python Join Examples

2015-09-08 10:12 941 查看
原文链接:点我


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