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

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

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

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

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

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

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:130
阅读量:41
阅读量:113
阅读量:126
阅读量:195