Python字符串| join()方法与示例
join()是Python中的一种内置方法,用于通过给定的str分隔符连接列表,字符串等元素。
注意:方法是使用分隔符调用的,并提供了arguments元素,然后最后一个(返回的)字符串是分隔符字符串连接的字符串。
语法:
String.join(iterable)
示例1:使用连字符('-')连接字符串
#s作为分隔符字符串
s = "-"
#str作为字符串序列
str = ("Hello", "World", "How are?")
#连接并打印字符串
print (s.join(str))输出结果
Hello-World-How are?
示例2:用空格连接字符串,将学生详细信息作为字符串序列提供
#s作为分隔符字符串 (contains - space) s = " " #学生资料 first_name = "Amit" second_name = "Shukla" age = "21" branch = "B.Tech" #创建字符串序列 str = (first_name, second_name, age, branch) #加入前str的值 print "str before join..." print (str) #连接并打印字符串 print "str after join..." print (s.join(str))
输出结果
str before join...
('Amit', 'Shukla', '21', 'B.Tech')
str after join...
Amit Shukla 21 B.Tech热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语