python 如何将字符串列表合并后转换成字符串? ''.join(List(str))函数
生活随笔
收集整理的這篇文章主要介紹了
python 如何将字符串列表合并后转换成字符串? ''.join(List(str))函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參考文章:python 怎么將列表轉換成字符串
temp_list = ['h', 'e', 'l', 'l', 'o'] result = ''.join(temp_list) print(result) # hellojoin函數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-o20200214
如果想實現字符串拼接,可使用:
# 用“+”連接字符串不建議使用,占用內存較大;建議使用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)總結
以上是生活随笔為你收集整理的python 如何将字符串列表合并后转换成字符串? ''.join(List(str))函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 编程笔记
- 下一篇: python opencv 如何获取图像