#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. Android实训案例(八)——单机五子棋游戏,自定义棋盘,线条,棋子,游戏逻辑,游戏状态存储,再来一局

    Android实训案例(八)--单机五子棋游戏,自定义棋盘,线条,棋子,游戏逻辑,游戏状态存储,再来一局 阿法狗让围棋突然就被热议了,鸿洋大神也顺势出了篇五子棋单机游戏的视频,我看到了就像膜拜膜拜,就 ...

  2. Linux System Programming --Chapter Eight

    内存管理 一.分配动态内存的几个函数 用户空间内存分配:malloc.calloc.realloc1.malloc原型如下:extern void *malloc(unsigned int num_b ...

  3. [转]Android长度单位详解

    android中定义的dimension单位有以下这些:px(Pixels ,像素):对应屏幕上的实际像素点.in(Inches ,英寸):屏幕物理长度单位.mm(Millimeters ,毫米):屏 ...

  4. 【面试笔试算法】Problem 1 : DP滑雪问题--网易互联网算法实习生2017笔试题

    Description Michael喜欢滑雪百这并不奇怪,因为滑雪的确很刺激.可是 为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  5. Linux进程管理 - ps,top,pstree,signal,kill,killall举例演示

    ps:将某个时间点的进程运行情况撷取下来 [root@www ~]# ps aux <==观察系统所有的进程数据 [root@www ~]# ps -lA <==也是能够观察所有系统的数据 ...

  6. iOS监听模式系列之对APNs的认知与理解

    前言: APNs 协议在近两年的 WWDC 上改过两次, 15 年 12 月 17 日更是推出了革命性的新特性.但在国内传播的博客.面试题里关于 APNs 的答案全都是旧的.错的. 导航: 对 APN ...

  7. 根据Facebook内存的管理使用,浅谈在iOS上自动检测内存泄漏问题

    分装库下载:https://github.com/facebook/FBMemoryProfiler FBMemoryProfiler类库使用教程:http://ifujun.com/fbmemory ...

  8. Android开发技巧——自定义控件之使用style

    Android开发技巧--自定义控件之使用style 回顾 在上一篇<Android开发技巧--自定义控件之自定义属性>中,我讲到了如何定义属性以及在自定义控件中获取这些属性的值,也提到了 ...

  9. android EventBus详解(一)

    EventBus 是一款针对Android优化的发布/订阅事件总线.主要功能是替代Intent, Handler, BroadCast 在 Fragment,Activity,Service,线程之间 ...

  10. LeetCode(36)- Implement Stack using Queues

    题目: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack ...