【转载】Python字符串操作之字符串分割与组合
1、 str.split():字符串分割函数
通过指定分隔符对字符串进行切片,并返回分割后的字符串列表。
语法:
str.split(s, num)[n]
参数说明:
s:表示指定的分隔符,不写的话,默认是空格(’ ‘)。如果字符串中没有给定的分隔符时,则把整个字符串作为列表的一个元素返回。
num:表示分割次数。如果指定了参数num,就会将字符串分割成num+1个子字符串,并且每一个子字符串可以赋给新的变量。
[n]:表示选取第n个分片,n表示返回的list中元素下标,从0开始的。
2、 os.path.split():路径文件分割函数
按照路径将文件名和路劲分割开,这里需要引入os包(import os)。
语法:
os.path.split(‘PATH’)
参数说明:
PATH指一个文件所在的绝对路径
实例:
1)split()函数常用的一些实例
#定义一个字符串str1
>>> str1 = "3w.gorly.test.com.cn" #使用默认分隔符分割字符串str1
>>> print str1.split()
['3w.gorly.test.com.cn'] #指定分隔符为'.',进行分割字符串str1
>>> print str1.split('.')
['3w', 'gorly', 'test', 'com', 'cn'] #指定分隔符为'.',并且指定切割次数为0次
>>> print str1.split('.',0)
['3w.gorly.test.com.cn'] #指定分隔符为'.',并且指定切割次数为1次
>>> print str1.split('.',1)
['3w', 'gorly.test.com.cn'] #指定分隔符为'.',并且指定切割次数为2次
>>> print str1.split('.',2)
['3w', 'gorly', 'test.com.cn'] #这种分割等价于不指定分割次数str1.split('.')情况
>>> print str1.split('.',-1)
['3w', 'gorly', 'test', 'com', 'cn'] #指定分隔符为'.',并取序列下标为0的项
>>> print str1.split('.')[0]
3w #指定分隔符为'.',并取序列下标为4的项
>>> print str1.split('.')[4]
cn
2)统计字符串中出现的单词个数
>>> str2 = "This is the voa special english health report"
>>> list1 = str2.split(' ')
>>> list1
['This', 'is', 'the', 'voa', 'special', 'english', 'health', 'report']
>>> len(list1)
8
3)、多次连续使用split()函数
例如:将从html代码中提取网站地址
>>> s = '<a href="www.test.com">test</a>'
>>> print s.split('"')[1]
www.test.com
>>> print s.split('"')[1].split('.')
['www', 'test', 'com']
4)、使用split()函数去除一些特殊字符
#去掉字符串中的换行符\n
>>> str2 = '''hello
... world
... !'''
>>> str2.split('\n')
['hello', 'world', '!']
5)、分割文件和其路劲
>>> import os
>>> print os.path.split("d:\test\a.txt")
('d:', '\test\x07.txt')
>>> print os.path.split('d:/test/a.txt')
('d:/test', 'a.txt')
>>> print os.path.split('d:\\test\\a.txt')
('d:\\test', 'a.txt')
从上面的结果可以看出,如果我们路劲写成d:\test\a.txt,是得不到我们想要的结果,必须将再加一个’\’来转义第二个’\’才行,或者直接写成d:/test/a.txt这样。
3、 str.join(seq):将序列组合成字符串函数
语法:s.join(seq)
参数说明:
s:给定的连接符
seq:代表要连接的序列,如list、tuple、str的序列
实例:
1)普通字符串的连接(只能针对字符或字符串进行连接)
>>> '-'.join("abdcd")
'a-b-d-c-d'
>>> list1 = ['a','b','c']
>>> ''.join(list1)
'abc'
2)字符串分割函数和字符串组合函数组合使用的情况
>>> s = '<a href="www.test.com">test</a>'
>>> print s.split('"')[1]
www.test.com
>>> print s.split('"')[1].split('.')
['www', 'test', 'com']
>>> print '.'.join(s.split('"')[1].split('.'))
www.test.com
原文转载于:https://blog.csdn.net/seetheworld518/article/details/47346527
【转载】Python字符串操作之字符串分割与组合的更多相关文章
- Python字符串操作之字符串分割与组合
12.字符串的分割和组合 12.1 str.split():字符串分割函数 通过指定分隔符对字符串进行切片,并返回分割后的字符串列表. 语法: str.split(s, num)[n] 参数说明: s ...
- awk字符串操作(字符串链接、传入传出shell变量)
1.awk基础 awk的环境变量及其意义 https://blog.csdn.net/snowpay/article/details/52451718 linux awk命令详解 https:// ...
- python基础操作_字符串操作_列表操作list
#字符串可以通过下表取值,如下程序 names='java python' print(names[0],names[5]) #使用for循环轮询所有name值 ''' for name in nam ...
- 【ORACLE】字符串操作 B字符串时A的一部分
select * from a where instr(a,b)>0;用于实现B字段是A字段中的某一部分的时候,要论顺序或者要相邻的字符. 如果想要不论顺序或者不相邻的字符时,定义函数可以实现: ...
- C#入门篇6-7:字符串操作 看看字符串的特殊之处 值类型与引用类型的区别
//看看字符串的特殊之处值类型与引用类型的区别 public static void CompareString(string stra, string strb, int i) { #region ...
- python基础--字符串操作、列表、元组、文件操作
一.变量及条件判断 1.字符串.布尔类型.float.int类型,None都是不可变变量 2.字符串是不可变变量,不可变变量就是指定义之后不能修改它的值 3.count +=1和count=count ...
- [Python Study Notes]字符串操作
字符串操作 a.字符串格式化输出 name = "liu" print "i am %s " % name #输出: i am liu PS: 字符 ...
- Python 字符串操作,截取,长度
1.字符串操作: 字符串长度: s = "; slen = len(s); 字符串截取: print s[:-:-] #截取,逆序隔1个取一个字符 print s[:-:-] #截取,逆序隔 ...
- python预课02 time模块,文本进度条示例,数字类型操作,字符串操作
time模块 概述:time库是Python中处理时间的标准库,包含以下三类函数 时间获取: time(), ctime(), gmtime() 时间格式化: strftime(), strptime ...
随机推荐
- 简明Docker教程
Docker基础 这篇基础文章是方便用户在使用cSphere平台之前,了解docker基础知识. 针对已经有一定的Linux基础知识的用户. Docker是什么 Docker是一个改进的容器技术.具体 ...
- 将数组划分成连续子序列 Split Array into Consecutive Subsequences
2018-08-04 20:47:43 问题描述: 问题描述: 本题需要的是将一个数组划分成子序列,保证每个子序列是连续的,并且长度要大于等于3. 解题思路是使用贪心算法,首先对数组中的数字进行计数, ...
- 15分钟入门lua
目录:[ - ] -- 1. Variables and flow control. -- 2. Functions. -- 3. Tables. -- 3.1 Metatables and meta ...
- English trip -- VC(情景课)1 F Another view
Another view 另一种观点 拓展应用 Life-skills reading 生活技能阅读 Midtown Adult School 中城成人学校 NAME: Samir Ahmed ...
- ubuntu安装环境软件全文档
1,安装apace2: sudo apt-get install apache2 2谷歌浏览器的安装:sudo apt-get install chromium-browser-dbg 3,国际版Q ...
- Oracle11g温习-第一章 3、ORACLE逻辑结构
2013年4月27日 星期六 10:27 Oracle逻辑结构的相关数据字典: 记录各个表空间的详细信息. SYS @ prod > select tablespace_name,status ...
- Leetcode 78
//和77类似的问题,可以放在一起记忆class Solution { public: vector<vector<int>> subsets(vector<int> ...
- 记一次使用utl_http方法调用接口,报字符或值错误
背景:ebs系统和其他系统通过utl_http包调用接口,使用log方法记录日志. 某次调用接口,执行到记录日志行报字符或值错误. 查找原因,发现是p_str的长度超过的32767的限制. 解决办法: ...
- learning uboot test command
uboot commad test test - minimal test like /bin/sh so we can use test command to some judge for exam ...
- 快速切题 sgu117. Counting 分解质因数
117. Counting time limit per test: 0.25 sec. memory limit per test: 4096 KB Find amount of numbers f ...