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)的更多相关文章

  1. python中字符串操作--截取,查找,替换

    python中,对字符串的操作是最常见的,python对字符串操作有自己特殊的处理方式. 字符串的截取 python中对于字符串的索引是比较特别的,来感受一下: s = '123456789' #截取 ...

  2. python中字符串拆分与合并——split()、join()、strip()和replace()

    Python3 split()方法 描述split()通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串 语法split()方法语法: str.split(str= ...

  3. VB中字符串操作函数

    Len Len(string|varname) 返回字符串内字符的数目,或是存储一变量所需的字节数. Trim Trim(string) 将字符串前后的空格去掉 Ltrim Ltrim(string) ...

  4. Python中字符串操作

    #Python字符串操作 '''1.复制字符串''' #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sStr1 = 'strcpy2' pri ...

  5. Python中列表操作函数append的浅拷贝问题

    L=int(input())#L位数N=int(input())#N进制row=[]list1=[]for i in range(1,N): row.append(1)list1.append(row ...

  6. python的字符串操作函数之一览

    s.strip(chars) s.find(x,start,end) s.index(x.start,end)#见上: s.format()#见上: s.partition(x)#见上: s.repl ...

  7. C/C++ 字符串操作函数 思维导图梳理

    这些常用的字符串操作函数都是包在string.h头文件中. 分享此图,方便大家记忆 <(^-^)> 选中图片点击右键,在新标签页中打开图片会更清晰

  8. Python中字符串String的基本内置函数与过滤字符模块函数的基本用法

    Python中字符串String的基本内置函数与用法 首先我们要明白在python中当字符编码为:UTF-8时,中文在字符串中的占位为3个字节,其余字符为一个字节 下面就直接介绍几种python中字符 ...

  9. JavaScript中常见的字符串操作函数及用法

    JavaScript中常见的字符串操作函数及用法 最近几次参加前端实习生招聘的笔试,发现很多笔试题都会考到字符串的处理,比方说去哪儿网笔试题.淘宝的笔试题等.如果你经常参加笔试或者也是一个过来人,相信 ...

随机推荐

  1. 查看scn headroom变化趋势的几种方法

    查看scn headroom变化趋势的几种方法 scn headroom问题,本文不做解释. 本文为自己的总结,脚本来自于oracle sr技术project师. 转载请注明出处http://blog ...

  2. HDU5567/BestCoder Round #63 (div.2) A sequence1 水

    sequence1  Given an array a with length n, could you tell me how many pairs (i,j) ( i < j ) for a ...

  3. Android EditText得到和失去焦点时,自定义处理内容

    当android的Edittext得到/失去焦点时,需要自定义一些处理内容时,需要对EditText对象的Focus进行监听处理. 在Activity中,做以下处理: 1.取得EditText对象. ...

  4. poj 2142

    Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of medicine. F ...

  5. UESTC--758--P酱的冒险旅途(模拟)

    P酱的冒险旅途 Time Limit: 1000MS   Memory Limit: 65535KB   64bit IO Format: %lld & %llu Submit Status ...

  6. 0428-mysql(事务、权限)

    1.事务 “事务”是一种可以保证“多条语句一次性执行完成”或“一条都不执行”的机制. 两种开始事务的方法: 1.set  autocommit = 0; //false,此时不再是一条语句一个事务了, ...

  7. 3.朴素贝叶斯和KNN算法的推导和python实现

    前面一个博客我们用Scikit-Learn实现了中文文本分类的全过程,这篇博客,着重分析项目最核心的部分分类算法:朴素贝叶斯算法以及KNN算法的基本原理和简单python实现. 3.1 贝叶斯公式的推 ...

  8. layui框架 各种小结

    首先项目前端采用的是bootstrap和layui弹窗,验证,表格用的是bootstrapTable layui官方地址:http://www.layui.com/ 文档:http://www.lay ...

  9. Python生成器(yield)

    对于调用一个普通的Python函数,一般是从函数的第一行代码开始执行,结束于return语句.异常或者函数所有语句执行完毕.一旦函数将控制权交还给调用者,就意味着全部结束.函数中做的所有工作以及保存在 ...

  10. # Nginx常见问题

    try_files的使用 按顺序检查文件是否 存在 location /{ try_files $uri $uri/ /index.php } 解析:在/下寻找$uri,如果没有找到,则去找$uri/ ...