程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python join() method

編輯:Python

List of articles

  • join() Method introduction
    • join() The syntax of the method is as follows :
    • 【 example 1】 Merge the strings in the list into one string .
    • 【 example 2】 Combine strings in tuples into one string .


join() Method introduction

join() Method is also a very important string method , It is split() The inverse of method , Used to list ( Or tuples ) Multiple strings contained in are concatenated into one string .

Want to learn more split() Method reader , Can be read 《Python split() Method 》 section .
Use join() Method to merge strings , It will list ( Or tuples ) Multiple strings in are connected together with fixed separators . for example , character string “c.biancheng.net” It can be seen as passing through the separator “.” take [‘c’,‘biancheng’,‘net’] The result of merging a list into a string .

join() The syntax of the method is as follows :

newstr = str.join(iterable)

The meanings of parameters in this method are as follows :

  • newstr: Represents the new string generated after the merge ;
  • str: Used to specify the separator when merging ;
  • iterable: Source string data for merge operation , Allow to list 、 Tuples and so on .

【 example 1】 Merge the strings in the list into one string .

>>> list = ['c','biancheng','net']
>>> '.'.join(list)
'c.biancheng.net'

【 example 2】 Combine strings in tuples into one string .

>>> dir = '','usr','bin','env'
>>> type(dir)
<class 'tuple'>
>>> '/'.join(dir)
'/usr/bin/env'

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved