Python字符串操作函数split()和join()
字符串拆分
在python中有切片(Slice)操作符,可以对字符串进行截取,还提供了split()函数可以将一个
字符串分裂成多个字符串组成的列表。在使用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.
"""
其语法为 string = str.split(sep,maxsplit)[n] ,该函数使用字符串中的字符作为分隔
符(sep),返回字符串分词后的列表(不含有作为分隔符的字符);同时还可以传入一个int参数
(n)作为分隔的次数,(默认值为 -1,不限制次数),maxsplit是分割次数。
需要注意的是,当不写分割符(sep)时表示所有的空字符,包括空格、换行(\n)、制表符(\t)等,
有分隔符时,以该分隔符进行分割。
实例演示
无分隔符的情况
str1 = "Process finished with exit code 0"
str2 = "Process\nfinished\nwith\nexit\ncode\n0"
str3 = "Process\tfinished\twith\texit\tcode\t0" str1 = str1.split()
str2 = str2.split()
str3 = str3.split() print(str1)
print(str2)
print(str3)

从运行结果可以看到,在没有参数的情况下,函数默认以空格,回车符,空格符等作为分割条件。
有分隔符的情况
str1 = "Process+finished+with+exit+code+0"
str2 = "Process,finished,with,exit,code,0"
str3 = "Process finished with exit code 0" str1 = str1.split('+')
str2 = str2.split(',')
str3 = str3.split('e') print(str1)
print(str2)
print(str3)

从运行结果可以看出,函数会以分隔符为分割条件,对字符串进行分割,再将得到的每个分割段
存储为列表进行返回。
有分割次数的情况
str2 = "Process,finished,with,exit,code,0"
sp1 = str2.split(',',1)
sp3 = str2.split(',',3)
sp5 = str2.split(',',5)
sp7 = str2.split(',',7)
print(sp1)
print(sp3)
print(sp5)
print(sp7)

该函数的第二个参数是分割次数,有同学可能发现了,sp7已经超出最大分割次数了啊。没错,
最后一个分割段是"0",以分隔符","对其无法再继续分割,所以保持不变。
简单应用
题目:在一行输入 3 个整数,用空格隔开,要求输出第二个整数的值。虽然是入门级的题,但很
好的运用了split()函数,大家可以自己先思考下再展开下面代码。

num = input().split(' ',2)[1]
print(num)
字符串合并
字符串合并在日常开发工作中比较常用,同样先看下其底层构造:
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
该函数可以连接任意数量的字符串,将字符串插入到被调用的两两字符串间,返回一个新的字符串。
其语法为 string_name.join(iterable) 。
参数说明:
string_name:分隔符,可以为空
iterable:要连接的元素序列、字符串、元组、字典
返回值:返回一个以分隔符string_name连接各个元素后生成的字符串
应用演示
操作字符串
str1 = 'python'
print('-'.join(str1))

操作序列
web = '.'
str2 = ["www","baidu","com"]
print("https://" + web.join(str2))

操作字典
str3 = {"name": "liulei","age": 19,"sex": 1}
print('-'.join(str3)) #操作字典时只对键进行连接

操作元祖
str4 = ("/home","test","scripts")
print("/".join(str4))

Python字符串操作函数split()和join()的更多相关文章
- python——字符串操作函数
字符串 join() map() split() rsplit() splitlines() partiton() rpartition() upper() lower() swapcase() ca ...
- Python 字符串操作函数一
#-*- coding:utf-8 -*- strword = "i will fly with you , fly on the sky ." #find print(strwo ...
- Python 字符串操作函数二
#-*- coding:utf-8 -*- line = "l want watch movie with you ." print(line.center(50)) print( ...
- Python中字符串操作函数string.split('str1')和string.join(ls)
Python中的字符串操作函数split 和 join能够实现字符串和列表之间的简单转换, 使用 .split()可以将字符串中特定部分以多个字符的形式,存储成列表 def split(self, * ...
- python字符串操作实方法大合集
python字符串操作实方法大合集,包括了几乎所有常用的python字符串操作,如字符串的替换.删除.截取.复制.连接.比较.查找.分割等,需要的朋友可以参考下: #1.去空格及特殊符号 s.st ...
- Lesson12——NumPy 字符串函数之 Part1:字符串操作函数
NumPy 教程目录 1 NumPy 字符串函数 以下函数用于对 dtype 为 numpy.string_ 或 numpy.unicode_ 的数组执行向量化字符串操作. 它们基于 Python 内 ...
- Python 字符串操作
Python 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) 去空格及特殊符号 s.strip() .lstrip() .rstrip(',') 复制字符 ...
- 转 Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
转自: http://www.cnblogs.com/huangcong/archive/2011/08/29/2158268.html 黄聪:Python 字符串操作(string替换.删除.截取. ...
- js 字符串操作函数有哪些
js 字符串操作函数有哪些 一.总结 一句话总结:js字符串函数都是字符串对象的方法,是通过调用字符串方法的方式调用,和java,php里面不一样. 1.字符串替换函数怎么用? 这里的正则表示是加双引 ...
- JavaScript中常见的字符串操作函数及用法
JavaScript中常见的字符串操作函数及用法 最近几次参加前端实习生招聘的笔试,发现很多笔试题都会考到字符串的处理,比方说去哪儿网笔试题.淘宝的笔试题等.如果你经常参加笔试或者也是一个过来人,相信 ...
随机推荐
- ModuleNotFoundError: No module named 'flask_login'
ModuleNotFoundError: No module named 'flask_login' 解决: pip install flask_login
- 关于JavaBean和vo的解释
前景提要 最近在学JavaWeb,接触到了很多java后端的概念,其中JavaBean和vo的概念一直让我模糊不清,查询众多资料后写个博客记录一下. 首先先贴一下两者的概念: JavaBean Jav ...
- 如何使用Map处理Dom节点
本文浅析一下为什么Map(和WeakMap)在处理大量DOM节点时特别有用. 我们在JavaScript中使用了很多普通的.古老的对象来存储键/值数据,它们处理的非常出色: const person ...
- nodejs和npm升级版本
由于服务器环境的不同可能需要根据实际情况升降对应的nodejs 及npm 版本,最简单的例子就是 npx 只适用于 npm 5+ 看想用npx 那不升级咋办呢,还有如error eslint@7.16 ...
- SpringBoot连接Redis失败报错:Unable to connect to Redis; (小白篇)
学习redis一段时间了,现在开始使用springboot整合redis,实现Java与redis数据库的连接与一系列的使用. 但刚开始就给我来了个下马威,直接寄,连接不上redis,在swagger ...
- opencv图像显示问题
opencv 的图像类型都是numpy array.dtype = uint8. 如果是默认的python的int类型的numpy array,即使每个整数都在范围0-255, 图像也不会显示,必须转 ...
- 【Python&RS】遥感影像的像素坐标转地理坐标(仿射变换)
GDAL(Geospatial Data Abstraction Library)是一个在X/MIT许可协议下的开源栅格空间数据转换库.它利用抽象数据模型来表达所支持的各种文件格式 ...
- 通过redis学网络(2)-redis网络模型
本系列主要是为了对redis的网络模型和集群原理进行学习,我会用golang实现一个reactor网络模型,并实现对redis协议的解析. 系列源码已经上传github https://github. ...
- docker构建FreeSWITCH编译环境及打包
操作系统 :CentOS 7.6_x64 FreeSWITCH版本 :1.10.9 Docker版本:23.0.6 FreeSWITCH这种比较复杂的系统,使用容器部署是比较方便的,今天 ...
- GO 集合 map 使用总结
转载请注明出处: Go语言的集合称为映射(map),它是一种无序的键值对(key-value)的集合,集合是通过键(key)来快速检索值(value)的,键(key)类似于索引,它指向值(value) ...