Python字符串处理的8招秘籍

开发 后端
Python的字符串处理,在爬虫的数据解析、大数据的文本清洗,以及普通文件处理等方面应用非常广泛,而且Python对字符串的处理内置了很多高效的函数,功能非常强大、使用非常方便。

 Python的字符串处理,在爬虫的数据解析、大数据的文本清洗,以及普通文件处理等方面应用非常广泛,而且Python对字符串的处理内置了很多高效的函数,功能非常强大、使用非常方便。今天我就把字符串处理时用到最多的方法总结分享给大家,希望大家可以轻松应对字符串处理。

[[273466]]

1.字符串的切片和相乘

(1)切片

  1. str='Monday is a busy day' 
  2. print(str[0:7])  #表示取第一个到第七个的字符串 
  3. print(str[-3:])  #表示取从倒数第三个字符开始到结尾的字符串 
  4. print(str[::])   #复制字符串 

(2)相乘

当我们编写Python代码时要分隔符,此时用字符串的乘法操作就很容易实现。

  1. line='*'*30 
  2. print(line) 
  3. >>****************************** 

2.字符串的分割

(1)普通的分割,用split函数,但是split只能做非常简单的分割,而且不支持多个分隔。

  1. phone='400-800-800-1234' 
  2. print(phone.split('-')) 
  3. >>['400''800''800''1234'

(2)复杂的分割,r表示不转义,分隔符可以是「;」,或者「,」,或者空格后面跟0个多个额外的空格,然后按照这个模式去分割。

  1. line='hello world; python, I ,like,    it' 
  2. import re 
  3. print(re.split(r'[;,s]\s*',line)) 
  4. >>>['hello world''python''I ''like''it'

3.字符串的连接和合并

(1)连接,两个字符可以很方便的通过“+”连接起来

  1. str1='Hello' 
  2. str2='World' 
  3. new_str=str1+str2 
  4. print(new_str) 
  5. >>>HelloWorld 

(2)合并,用join方法

  1. url=['www','python','org'
  2. print('.'.join(url)) 
  3. >>>www.python.org 

4.判断字符串是否以指定前缀、后缀结尾

假设我们要查一个文件的名字是以什么开头或者什么结尾?

  1. filename='trace.h' 
  2. print(filename.endswith('h')) 
  3. >>True 
  4. print(filename.startswith('trace')) 
  5. >>True 

5.字符串的查找和匹配

(1)一般查找

利用find方法可以很方便的在长的字符串里面查找子字符串,会返回字符串所在位置的索引,若找不到返回-1

  1. str1 = "this is string example....wow!!!" 
  2. str2 = "exam" 
  3. print(str1.find(str2))      # 15 
  4. print(str1.find(str2, 10))  # 15 
  5. print(str1.find(str2, 40))  # -1 

(2)复杂的匹配,就需要用到正则表达式。

  1. mydate='11/27/2016' 
  2. import re 
  3. if re.match(r'\d+/\d+/\d+',mydate): 
  4.     print('ok.match'
  5. else
  6.     print('not match'
  7.  
  8. >>>ok.match 

6.统计字符串里某个字符出现的次数

  1. str = "thing example....wow!!!" 
  2. print(str.count('i', 0, 5))  # 1 
  3. print(str.count('e'))  # 2 

7.字符串的替换

(1)普通的替换,用replace方法就可以了

  1. text='python is an easy to learn,powerful programming language.' 
  2. print(text.replace('learn','study')) 
  3. >>>python is an easy to study,powerful programming language. 

(2)复杂的替换,需要用到re模块的sub函数

  1. students='Boy 103,girl 105' 
  2. import re 
  3. print(re.sub(r'\d+','100',students)) 
  4. >>>Boy 100,girl 100 

8.去掉字符串中一些特定的字符

(1)去空格,对文本处理的时候比如从文件中读取一行,然后需要去除每一行的空格、table或者是换行符。

  1. str = ' python str ' 
  2. print(str) 
  3. # 去首尾空格 
  4. print(str.strip()) 
  5. # 去左侧空格 
  6. print(str.lstrip()) 
  7. # 去右侧空格 
  8. print(str.rstrip()) 

(2)复杂的文本清理,可以利用str.translate。

比如先构建一个转换表,table是一个翻译表,表示把“to”转成大写的“TO”,然后在old_str里面去掉‘12345’,然后剩下的字符串再经过table翻译。

  1. instr = 'to' 
  2. outstr = 'TO' 
  3. old_str = 'Hello world , welcome to use Python. 123456' 
  4. remove = '12345' 
  5. table = str.maketrans(instr,outstr,remove) 
  6. new_str = old_str.translate(table
  7. print(new_str) 
  8. >>>HellO wOrld , welcOme TO use PyThOn. 6 

总结

平时我们使用Python都是处理一些脚本,其中使用频率最大的就是字符串的处理方面,因此给大家整理了这些常用的字符串处理时使用的方法,希望对大家有用。

责任编辑:华轩 来源: googpy
相关推荐

2023-10-18 07:55:41

Python字符串

2010-11-26 09:51:54

MySQL字符串

2010-07-14 16:35:52

Perl字符串处理函数

2010-03-10 15:06:16

python字符串

2020-05-12 08:53:15

JavaScript字符串处理库

2021-08-26 11:41:50

字符串String.jsVoca

2010-11-26 11:20:31

MySQL字符串处理函

2010-08-04 11:23:15

Flex字符串

2016-12-30 13:32:24

字符串算法代码

2009-11-26 16:26:32

PHP字符串mbstr

2023-12-15 10:27:01

暴力匹配算法Python字符串

2010-03-19 13:57:30

Python字符串处理

2010-10-09 11:54:46

MySQL字符串

2010-07-19 15:07:46

Perl字符串处理函数

2023-08-21 10:28:00

字符串字符Python

2023-08-26 20:21:58

字符KotlinJava

2010-03-09 16:16:55

Python字符串

2010-03-09 15:15:02

Python字符串类型

2024-02-26 08:00:00

Pythonformat()字符串

2016-12-30 13:16:51

字符串算法代码
点赞
收藏

51CTO技术栈公众号