#join() 与split()相反,join()方法用来将列表中多个字符串进行连接,并在相邻两个字符串之间插入指定字符
li=['apple','peach','banana','pear']
sep=','
s=sep.join(li)
print(s) #使用逗号作为连接符
s1=':'.join(li) #使用冒号作为连接符
print(s1)
s2=''.join(li)
print(s2)
#使用split()和join()方法可以删除字符串中多余的空白字符,如果有连续多个空白字符,只保留一个
x='aaa bb c d e fff'
print(' '.join(x.split())) def equalilent(s1,s2): #判断两个字符串在python意义上是否等价
if s1 == s2:
return True
elif ' '.join(s1.split()) == ' '.join(s2.split()):
return True
elif ''.join(s1.split()) == ''.join(s2.split()):
return True
else:
return False
print(equalilent('pip list','pip list'))
# True
print(equalilent('[1,2,3]','[1,2,3]')) #判断两个列表写法是否等价
# True
print(equalilent('[1,2,3]','[1,2, 3]'))
# True
print(equalilent('[1,2,3','[1,2 ,3,4]'))
# False
'''使用运算符"+"也可以连接字符串,但该运算符设计大量数据的复制,效率非常低,不适合大量长字符串的连接。''' import timeit #使用列表推导式生成10000个字符串
strlist = ['This is a long string that will not keep in memory.' for n in range(10000)] #使用字符串对象的join()方法连接多个字符串
def use_join():
return ''.join(strlist) #使用运算符"+"连接多个字符串
def use_plus():
result=''
for strtemp in strlist:
result = result+strtemp
return result if __name__=='__main__':
#重复运行次数
times=1000
jointimer = timeit.Timer('use_join()','from __main__ import use_join')
print('time for join:',jointimer.timeit(number=times))
# time for join: 0.1647309590189252
plustimer = timeit.Timer('use_plus()','from __main__ import use_plus')
print('time for plus:',plustimer.timeit(number=times))
# time for plus: 2.045372327003861

Python_字符串连接的更多相关文章

  1. 关于python字符串连接的操作

    python字符串连接的N种方式 注:本文转自http://www.cnblogs.com/dream397/p/3925436.html 这是一篇不错的文章 故转 python中有很多字符串连接方式 ...

  2. js ES6 多行字符串 连接字符串

    1. 以前,js多行字符串用\n写起来比较费事,所以最新的ES6标准新增了一种多行字符串的表示方法,用` ... `表示: 旧版写法 alert("你好,\n 我叫\n Olive" ...

  3. python字符串连接的N种方式

    python中有很多字符串连接方式,今天在写代码,顺便总结一下: 最原始的字符串连接方式:str1 + str2 python 新字符串连接语法:str1, str2 奇怪的字符串方式:str1 st ...

  4. SQL注入的字符串连接函数

    在select数据时,我们往往需要将数据进行连接后进行回显.很多的时候想将多个数据或者多行数据进行输出的时候,需要使用字符串连接函数.在sqli中,常见的字符串连接函数有concat(),group_ ...

  5. HDU 1000 & HDU1001 & 字符串连接

    A + B Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  6. Swift语言—有趣的字符串连接、数组、字典

    字符串链接:Swift语言中的字符串连接方式本人觉得非常的有趣,变量连接需要用右斜杠,并且变量名要括起来 “\(变量名)”,后面的字符串连接分别用逗号 ‘ , ’ 隔开 数组: Var arr = [ ...

  7. python 字符串连接

    字符串连接 方法1: 用字符串的join方法 a = ['a','b','c','d']content = ''content = ''.join(a)print content 方法2: 用字符串的 ...

  8. C 语言字符串连接的 3种方式

    C 语言字符串连接的 3种方式 #include<stdio.h> #include<stdlib.h> #include<string.h> char *join ...

  9. php大力力 [024节]PHP中的字符串连接操作(2015-08-27)

    2015-08-27 php大力力024.PHP中的字符串连接操作 PHP中的字符串连接操作  阅读:次   时间:2012-03-25 PHP字符串的连接的简单实例 时间:2013-12-30 很多 ...

随机推荐

  1. How To Get Log, Trace Files In OA Framework Pages And Concurrent Request Programs

    Goal   Solution   References APPLIES TO: Oracle Supplier Lifecycle Management - Version 12.1.2 and l ...

  2. 非阻塞IO模式原理

    与阻塞模式对应的另一种模式叫非阻塞IO模式,在整个通信过程中读和写操作不会阻塞,当前处理线程不存在阻塞情况.从A机器到B机器它的通信过程是:A机器一条线程将通道设置为写事件后往下执行,而另外一条线程遍 ...

  3. 加载SpriteBuilder中的scene为何不能带后缀

    我们在Xcode中切换SpriteBuilder中的scene时,一般使用的是如下代码: -(void)exitButtonPressed:(CCControl*)sender{ CCLOG(@&qu ...

  4. LeetCode之“动态规划”:Triangle

    题目链接 题目要求: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to ...

  5. "《算法导论》之‘字符串’":字符串匹配

    本文主要叙述用于字符串匹配的KMP算法. 阮一峰的博文“字符串匹配的KMP算法"将该算法讲述得非常形象,可参考之. 字符串‘部分匹配值’计算 KMP算法重要的一步在于部分匹配值的计算.模仿& ...

  6. 在 Vim 中设置 Tab 为4个空格

    缩进用 tab 制表符还是空格,这不是个问题,就像 python 用四个空格来缩进一样,这是要看个人喜好的.在 Vim 中可以很方便的根据不同的文件类型来设置使用 tab 制表符或者空格,还可以设置长 ...

  7. objc写一个NSMutableArray不连续索引替换对象的方法

    NSMutableArray内置的方法-(void)replaceObjectsAtIndexes:(NSIndexSet*)set withObjects:(NSArray*)objs 只能替换一段 ...

  8. iOS和Android开发异同点(一)

    说到移动开发,目前主流平台有谷歌的android os 系统,苹果iOS系统,和微软主打的windows Phone OS 系统,至于目前为啥移动开发中,安卓和iOS比较受欢迎,者要看三家产品的历史由 ...

  9. Qt Creator 更改默认构建目录到工程目录下

    Qt Creator 更改默认构建目录到工程目录下 步骤 工具->选项->构建和运行->概要->Default build directory->去掉第一个". ...

  10. python笔记--1

    pip工具常用命令: pip命令示例 说明 pip download SomePackage[==version] 下载扩展库的指定版本,不安装 pip freeze [> requiremen ...