freeCodeCamp/guide/chinese/python/string-methods/string-join-method/index.md

1.2 KiB
Raw Blame History

title localeTitle
String Join Method 字符串连接方法

字符串连接方法

str.join(iterable)方法用于连接具有指定字符串striterable所有元素。 如果iterable包含任何非字符串值则会引发TypeError异常。

iterable :字符串的所有迭代。可以是字符串列表,字符串元组甚至是普通字符串。

例子

1":"加入字符串ist

print ":".join(["freeCodeCamp", "is", "fun"]) 

产量

freeCodeCamp:is:fun 

2" and "加入一个字符串元组

print " and ".join(["A", "B", "C"]) 

产量

A and B and C 

3在字符串中的每个字符后面插入一个" "

print " ".join("freeCodeCamp") 

输出:

free C ode C amp 

4加入空字符串。

list1 = ['p','r','o','g','r','a','m'] 
 print("".join(list1)) 

输出:

program 

5加入套装。

test =  {'2', '1', '3'} 
 s = ', ' 
 print(s.join(test)) 

输出:

2, 3, 1 

更多信息:

字符串连接的Python文档