网站首页 美食营养 游戏数码 手工爱好 生活家居 健康养生 运动户外 职场理财 情感交际 母婴教育 时尚美容

如何用python拼接字符串,去空格

时间:2024-10-11 18:47:49

1、直接用“+”进行拼接,优点是简洁,缺点是当拼接的字符串多的情况下,效率低下

如何用python拼接字符串,去空格

2、采用join方法,将list中的元素拼接成字符串拼接

如何用python拼接字符串,去空格

3、使用reduce函数进行字符串的#!/usr/bin/env pythonfrom functools import reduc髫潋啜缅eimport operatorlist1 = ['hello',' ','world']result = reduce(operator.add,list1,'')print result

如何用python拼接字符串,去空格

4、python去掉空格方法#!/usr/bin/env pythonstr1 = "a b d e d f g"str1 =str1.replace(' ','')print(str1)

如何用python拼接字符串,去空格

5、取字符串第一个字符#!/usr/bin/env pythonstr1 = "a b d e d f g"str1 =str1.replace(' ','')print(str1[0])

如何用python拼接字符串,去空格

6、取字符串最后一个字符#!/usr/bin/env pythonstr1 = "a b d e d f g"str1 =str1.replace(' &#泌驾台佐39;,'')print(str1[-1])

如何用python拼接字符串,去空格
© 2025 小知经验
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com