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. 《Pro Android Graphics》读书笔记之第二节

    Android Digital Video: Formats, Concepts and Optimization Android Digital Video Formats: MPEG4 H.264 ...

  2. BasePath问题-nginx负载均衡配置

    在配置nginx+tomcat好后.将项目加入到webapps中.发现訪问主页时,css与js訪问不到,导致主页布局出错.细致分析原因后发现css与js的地址是basePath得出的.而basePat ...

  3. string[][]和string[,] 以及 int[][]和int[,]

    string[][]和string[,] http://www.codewars.com/kata/56f3a1e899b386da78000732/train/csharp Write a func ...

  4. Android入门之文件系统操作(二)文件操作相关指令

    (一)获取总根 File[] fileList=File.listRoots(); //返回fileList.length为1 //fileList.getAbsolutePath()为"/ ...

  5. EJB是什么鸟东西?

    到底EJB是什么 到底EJB是什么?被口口相传的神神秘秘的,百度一番,总觉得没有讲清楚的,仍觉得一头雾水.百度了很久,也从网络的文章的只言片语中,渐渐有了头绪. 用通俗话说,EJB就是:"把 ...

  6. python统计ES存储空间占用的代码

    import os from os.path import join, getsize def get_dir_size(dir, suffix_filter=None): size = 0L if ...

  7. E20170907-ts

    flash  vt. 使闪光,使闪烁; 拍出,发出(电报等); 〈口〉炫耀;          adj. 闪光的,闪耀的,一闪而过的; 浮华的; 庞大的;           n. 闪光; 闪光灯下摄 ...

  8. 没调出来 P2023

    #include<iostream> #include<cstdio> #include<cstring> #define ll long long #define ...

  9. [Apple开发者帐户帮助]六、配置应用服务(3)创建地图标识符和私钥

    要与MapKit JS通信,您将使用Maps私钥对一个或多个开发人员令牌进行签名. 首先注册地图标识符以识别您的应用.为使用MapKit JS的每个应用注册地图标识符.接下来创建并下载启用了MapKi ...

  10. chapter6 数据结构基础之习题 Parentheses Balance

    You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...