Python字符串操作之字符串分割与组合

Python字符串操作之字符串分割与组合


12.1 str.split():字符串分割函数 
通过指定分隔符对字符串进行切片,并返回分割后的字符串列表。 
语法: 
str.split(s, num)[n] 
参数说明: 
s:表示指定的分隔符,不写的话,默认是空格(’ ‘)。如果字符串中没有给定的分隔符时,则把整个字符串作为列表的一个元素返回。 
num:表示分割次数。如果指定了参数num,就会将字符串分割成num+1个子字符串,并且每一个子字符串可以赋给新的变量。 
[n]:表示选取第n个分片,n表示返回的list中元素下标,从0开始的。

12.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
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

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
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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']
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

4)、使用split()函数去除一些特殊字符

#去掉字符串中的换行符\n
>>> str2 = '''hello
... world
... !'''
>>> str2.split('\n')
['hello', 'world', '!']

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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')
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

从上面的结果可以看出,如果我们路劲写成d:\test\a.txt,是得不到我们想要的结果,必须将再加一个’\’来转义第二个’\’才行,或者直接写成d:/test/a.txt这样。

12.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'
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

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
原文地址:http://blog.csdn.net/seetheworld518/article/details/47346527
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值