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 生日广州男生祝福语简短