Python_字符串连接
#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_字符串连接的更多相关文章
- 关于python字符串连接的操作
python字符串连接的N种方式 注:本文转自http://www.cnblogs.com/dream397/p/3925436.html 这是一篇不错的文章 故转 python中有很多字符串连接方式 ...
- js ES6 多行字符串 连接字符串
1. 以前,js多行字符串用\n写起来比较费事,所以最新的ES6标准新增了一种多行字符串的表示方法,用` ... `表示: 旧版写法 alert("你好,\n 我叫\n Olive" ...
- python字符串连接的N种方式
python中有很多字符串连接方式,今天在写代码,顺便总结一下: 最原始的字符串连接方式:str1 + str2 python 新字符串连接语法:str1, str2 奇怪的字符串方式:str1 st ...
- SQL注入的字符串连接函数
在select数据时,我们往往需要将数据进行连接后进行回显.很多的时候想将多个数据或者多行数据进行输出的时候,需要使用字符串连接函数.在sqli中,常见的字符串连接函数有concat(),group_ ...
- HDU 1000 & HDU1001 & 字符串连接
A + B Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Swift语言—有趣的字符串连接、数组、字典
字符串链接:Swift语言中的字符串连接方式本人觉得非常的有趣,变量连接需要用右斜杠,并且变量名要括起来 “\(变量名)”,后面的字符串连接分别用逗号 ‘ , ’ 隔开 数组: Var arr = [ ...
- python 字符串连接
字符串连接 方法1: 用字符串的join方法 a = ['a','b','c','d']content = ''content = ''.join(a)print content 方法2: 用字符串的 ...
- C 语言字符串连接的 3种方式
C 语言字符串连接的 3种方式 #include<stdio.h> #include<stdlib.h> #include<string.h> char *join ...
- php大力力 [024节]PHP中的字符串连接操作(2015-08-27)
2015-08-27 php大力力024.PHP中的字符串连接操作 PHP中的字符串连接操作 阅读:次 时间:2012-03-25 PHP字符串的连接的简单实例 时间:2013-12-30 很多 ...
随机推荐
- 关于Android自定义view 你所需要知道的基本函数
开始时之前想吐槽一句..iphone的闹钟,12小时制.我成功的把闹钟订到了下午5:00 导致错过一班飞机.心疼改签费. 候机ing,没有事做,来写一下学习自定义view必须掌握的基本函数.这里只挑一 ...
- LeetCode之“树”:Symmetric Tree && Same Tree
Symmetric Tree 题目链接 题目要求: Given a binary tree, check whether it is a mirror of itself (ie, symmetric ...
- 传输控制协议(TCP) -- 连接建立及终止过程
TCP简介 相对于不可靠.无连接的用户数据报协议(User Datagram Protocol, UDP),传输控制协议(Transmission Control Protocol, TCP)是可靠的 ...
- 【一天一道LeetCode】#8. String to Integer (atoi)
一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...
- C语言笔试经典-查找多位数重复数字以及次数
从键盘输入一个多位的整数 用程序判断 这个数里面有没有 重复的数字 有重复的数字就打印 哪个数字重复了 重复了几次 例如:输入:1122431 打印结果: 1重复 出现3次 2重复 出现2次, ...
- Unity UGUI
超详细的基础教程传送门:(持续更新中) Unity UGUI之Canvas&EventSystem:http://blog.csdn.net/qq992817263/article/detai ...
- linux下64位汇编的系统调用(4)
经过上一篇的铺垫貌似可以很轻松的用汇编写出mmap的代码来,可仔细一看,还是有不少问题需要解决: 1.系统调用mmap如果出错并不直接返回MAP_FAILED(-1),而是一个"类似&quo ...
- unix设计哲学
说到Unix为我们所带来的软件开发的哲学,我必需要说一说.Unix遵循的原则是KISS(Keep it simple, stupid).在http://en.wikipedia.org/wiki/Un ...
- win32多线程学习笔记
<多核程序设计技术> 第五章--线程api,一个使用windows事件的线程应用程序,vs2008下编译调试通过. // 线程通信机制.cpp : 定义控制台应用程序的入口点. // #i ...
- 我的摸索过程之IIS下配置asp.net 的注意事项
"在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的.如果在 IIS 中没有将虚拟目录配置为应用程序,则可能导致此错误. ...