Python中的join()函数

原网址:https://www.geeksforgeeks.org/join-function-python

Python中的join()函数

join()是一个字符串方法,它返回被子字符串连接的字符串。

语法:

string_name.join(iterable)
string_name:这是被连接的子字符串。

参数:The join() method takes join()方法需要可迭代的元素来一次返回它的一个成员,比如列表,元组,字符串,字典和集合

返回值: join()方法返回一个被子字符串连接的字符串。

Type Error: 如果这个可迭代元素包含任何不是字符串的值,join()函数就会抛出TypeError。

下面的程序解释了join()方法是如何工作的:

 # Python program to demonstrate the 
# use of join function to join list 
# elements with a character. 
  
list1 = ['1','2','3','4']  
  
s = "-"
  
# joins elements of list1 by '-' 
# and stores in sting s 
s = s.join(list1) 
  
# join use to join a list of 
# strings to a separator s 
print(s) 

输出:

1-2-3-4

用空字符连接

# Python program to demonstrate the 
# use of join function to join list 
# elements without any separator. 
  
# Joining with empty separator 
list1 = ['g','e','e','k', 's']  
print("".join(list1)) 

输出:

geeks
  • 174
    点赞
  • 726
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值