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中常见的字符串操作函数及用法 最近几次参加前端实习生招聘的笔试,发现很多笔试题都会考到字符串的处理,比方说去哪儿网笔试题.淘宝的笔试题等.如果你经常参加笔试或者也是一个过来人,相信 ...
随机推荐
- DOS命令将黑框中查询到的信息保存到TXT等文件里
- Erlang下与其他程序和语言的通信机制(1)
在Erlang运行时中,提供了几种机制来实现与其它程序或者语言的通信.一种为分布式Erlang,一种为端口,其中端口分为普通端口和链入式驱动端口,还有后面引入的NIFs. 分布式Erlang:一个Er ...
- 上机题目(0基础)- 数据库事务(Java)
/* * 文件名称:JDBCTestCase.java * 版权:Copyright 2006-2011 Huawei Tech. Co. Ltd. All Rights Reserved. * 描写 ...
- mysql-5.5 for linux源码安装
mysql-5.5 for linux源码安装 1.使用Yum安装依赖软件包 # yum install -y gcc gcc-c++ gcc-g77 autoconf automake bison ...
- Java图形用户界面编程
1.Java图形用户界面编程概述 JavaAPI中提供了两套组件用于支持编写图形用户界面:AWT(抽象窗口包)和Swing 2. 容器(Container):重量级容器和轻量级容器(一个容器可以放置 ...
- etcd数据备份与恢复验证
一.单机 说明:执行etcd备份数据的恢复的机器必须和原先etcd所在机器一致 1.单机备份 etcdctl --endpoints="https://10.25.72.62:2379&qu ...
- layui日期输入框
<div class="layui-form-item"> <label class="layui-form-label& ...
- Java.Cas4.0客户端接入配置
1.本示例中使用cas4.0+cas-client-core3.4版本,ide则是myeclipse2014 blue版本,mvn使用3.9版本 2.注意事项:请先实现破解cas-client-cor ...
- 题解 UVA10587 【Mayor's posters】
先讲一下:dalao @lisuier 发布的前一篇题解严格来讲是有错误的 比如下一组数据: 1 3 1 10 1 4 7 10 显然答案是3,然而用lisuier dalao的程序做出来的答案是2( ...
- lsit集合去重复 顶级表达式
updateList = updateList.Where((x, i) => updateList.FindIndex(z => z.ID == x.ID) == i).ToList() ...