700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python 如何将字符串列表合并后转换成字符串? ''.join(List(str))函数

python 如何将字符串列表合并后转换成字符串? ''.join(List(str))函数

时间:2020-10-30 02:12:49

相关推荐

python 如何将字符串列表合并后转换成字符串? ''.join(List(str))函数

参考文章:python 怎么将列表转换成字符串

temp_list = ['h', 'e', 'l', 'l', 'o']result = ''.join(temp_list)print(result) # hello

join函数doc:

def join(self, iterable): # real signature unknown; restored from __doc__"""S.join(iterable) -> strReturn a string which is the concatenation of the strings in theiterable. The separator between elements is S.返回一个字符串,该字符串是迭代器(可迭代对象)中字符串的串联。 元素之间的分隔符是S。"""return ""

例如:

temp_list = ['h', 'e', 'l', 'l', 'o']result = '-'.join(temp_list)print(result) # h-e-l-l-o

0214

如果想实现字符串拼接,可使用:

# 用“+”连接字符串不建议使用,占用内存较大;建议使用join()方法,用''将可迭代字符串连接起来# obj_strs = obj_strs + class_num + (' {:.6f} {:.6f} {:.6f} {:.6f}\n'.format(c1, c2, d1, d2))obj_strs = ''.join([obj_strs, class_num, ' {:.6f} {:.6f} {:.6f} {:.6f}\n'.format(c1, c2, d1, d2)])print(obj_strs)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。