Python中字符串操作函数string.split('str1')和string.join(ls)
Python中的字符串操作函数split 和 join能够实现字符串和列表之间的简单转换,
使用 .split()可以将字符串中特定部分以多个字符的形式,存储成列表
def split(self, *args, **kwargs): # real signature unknown
"""
Return a list of the words in the string, using sep as the delimiter string. sep
The delimiter according which to split the string.
None (the default value) means split according to any whitespace,
and discard empty strings from the result.
maxsplit
Maximum number of splits to do.
-1 (the default value) means no limit.
"""
pass
如上所说,split()函数,使用字符串中的字符作为分隔符(sep),返回字符串分词的列表(不含有作为分隔符的字符),如果分隔符为None,则以字符串中的空格作为分隔符;同时还可以传入一个int参数
作为分隔的次数,(默认值 为 -1,不限制次数)
eg:
>>>:s = 'Process finished with exit code 0'
>>>:s.split()# sep:None;maxsplit:-1.空格作为分隔符,分隔所有
['Process', 'finished', 'with', 'exit', 'code', ''] >>>:s.split('i')#sep:‘i’;maxsplit:-1.‘i’作为分隔符,分割所有
['Process f', 'n', 'shed w', 'th ex', 't code 0'] >>>:s.split('i', 2)#sep:‘i’;maxsplit:2.‘i’作为分隔符,分割两次
['Process f', 'n', 'shed with exit code 0']
再进一步:
>>>:s = 'Process finished with exiiit code 0' >>>:s.split('i')
['Process f', 'n', 'shed w', 'th ex', '', '', 't code 0']
使用join()可以将列表中的字符串类型数据,组合成一个字符串:
def join(self, ab=None, pq=None, rs=None): # real signature unknown; restored from __doc__
"""
Concatenate any number of strings. The string whose method is called is inserted in between each given string.
The result is returned as a new string. Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
"""
pass
如上所说连接任意数量的字符串,将字符串插入到被调用的两两字符串间,返回一个新的字符串。
总结:
s = 'abcdabcd' ls = s.split('c') s2 = 'c'.join(ls) s = s2
上面利用两个函数互相转化,使用split()由字符串得到新列表,再使用join()由列表得到新字符串,
Python中字符串操作函数string.split('str1')和string.join(ls)的更多相关文章
- python中字符串操作--截取,查找,替换
python中,对字符串的操作是最常见的,python对字符串操作有自己特殊的处理方式. 字符串的截取 python中对于字符串的索引是比较特别的,来感受一下: s = '123456789' #截取 ...
- python中字符串拆分与合并——split()、join()、strip()和replace()
Python3 split()方法 描述split()通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串 语法split()方法语法: str.split(str= ...
- VB中字符串操作函数
Len Len(string|varname) 返回字符串内字符的数目,或是存储一变量所需的字节数. Trim Trim(string) 将字符串前后的空格去掉 Ltrim Ltrim(string) ...
- Python中字符串操作
#Python字符串操作 '''1.复制字符串''' #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sStr1 = 'strcpy2' pri ...
- Python中列表操作函数append的浅拷贝问题
L=int(input())#L位数N=int(input())#N进制row=[]list1=[]for i in range(1,N): row.append(1)list1.append(row ...
- python的字符串操作函数之一览
s.strip(chars) s.find(x,start,end) s.index(x.start,end)#见上: s.format()#见上: s.partition(x)#见上: s.repl ...
- C/C++ 字符串操作函数 思维导图梳理
这些常用的字符串操作函数都是包在string.h头文件中. 分享此图,方便大家记忆 <(^-^)> 选中图片点击右键,在新标签页中打开图片会更清晰
- Python中字符串String的基本内置函数与过滤字符模块函数的基本用法
Python中字符串String的基本内置函数与用法 首先我们要明白在python中当字符编码为:UTF-8时,中文在字符串中的占位为3个字节,其余字符为一个字节 下面就直接介绍几种python中字符 ...
- JavaScript中常见的字符串操作函数及用法
JavaScript中常见的字符串操作函数及用法 最近几次参加前端实习生招聘的笔试,发现很多笔试题都会考到字符串的处理,比方说去哪儿网笔试题.淘宝的笔试题等.如果你经常参加笔试或者也是一个过来人,相信 ...
随机推荐
- React-Router 中文简明教程(上)
概述 说起 前端路由,如果你用过前端 MV* 框架构建 SPA 应用(单页面应用),对此一定不陌生. 传统开发中的 路由,是由服务端根据不同的用户请求地址 URL,返回不同内容的页面,而前端路由则将这 ...
- log4net preserveLogFileNameExtension 和 watch
preserveLogFileNameExtension <log4net> <appender name="fileappender" type="l ...
- ningbooj--1655--木块拼接(贪心)
[1655] 木块拼接 时间限制: 1000 ms 内存限制: 65535 K 问题描述 好奇的skyv95想要做一个正方形的木块,现在有三种颜色的矩形木块,颜色分别为"A" ...
- 自顶向下(递归)的归并排序和自底向上(循环)的归并排序——java实现
归并排序有两种实现方式,自顶向下和自底向上.前者的思想是分治法,现将数组逐级二分再二分,分到最小的两个元素后,逐级往上归并,故其核心在于归并.后者的思想相反,采用循环的方式将小问题不断的壮大,最后变成 ...
- Oracle Instant Client 安装配置
一.下载 下载地址:http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html 这是Ora ...
- yii widget使用的3个用法
yii视图中使用的widget方式总结:常用的有3种方式:一.显示详细信息: $this->widget('zii.widgets.CDetailView', array( 'data' =&g ...
- angular的directive指令的link方法
比如 指令标签 <mylink myLoad="try()"></mylink> link:function(scope,element,attr){ el ...
- Asp.net MVC Checkbox控件 和 Nullable<bool>, 或bool?类型
@Html.CheckBoxFor() 这个方法生成两个Input HTML标签,不明白为什么这样,如果数据库是Nullable<bool>类型,就会报错. 网上的解决方法是这样: 方法一 ...
- angularJS之ng-bind与ng-bind-template的区别
ng-bind-template 指令用于告诉 AngularJS 将给定表达式的值替换 HTML 元素的内容. 当你想在 HTML 元素上绑定多个表达式时可以使用 ng-bind-template ...
- Android Fragment与Activity交互的几种方式
这里我不再详细介绍那写比较常规的方式,例如静态变量,静态方法,持久化,application全局变量,收发广播等等. 首先我们来介绍使用Handler来实现Fragment与Activity 的交互. ...