Python描述数据结构之字符串篇

前言

  字符串通常被称为串,前面博客也提到过,它是Python的一种基本数据类型,所以在本篇博文不再对字符串做过的叙述,而是整理一下Python中内置的对字符串的操作方法。

1. count()

   s t r . c o u n t ( s u b [ , s t a r t [ , e n d ] ] ) str.count(sub[, start[, end]]) str.count(sub[,start[,end]])
  统计子字符串 s u b sub sub [ s t a r t , e n d ] [start, end] [start,end]范围内出现的次数。可选参数 s t a r t start start e n d end end是切片表示法。

	str_x = 'abcdbbefg'
    print(str_x.count('bb'))
>>>>1

2. encode()

   s t r . e n c o d e ( e n c o d i n g = " u t f − 8 " , e r r o r s = " s t r i c t " ) str.encode(encoding="utf-8", errors="strict") str.encode(encoding="utf8",errors="strict")
  对字符串按指定编码进行编码。 默认编码为 ′ u t f − 8 ′ 'utf-8' utf8。可以通过 e r r o r s errors errors来设置不同的错误处理方案, e r r o r s errors errors的默认值为 ′ s t r i c t ′ 'strict' strict,表示编码错误会引发 U n i c o d e E r r o r UnicodeError UnicodeError,其他可用的值为 ′ i g n o r e ′ 'ignore' ignore ′ r e p l a c e ′ 'replace' replace

	str_x = 'abcdbbefg'
    print(str_x.encode())
>>>>b'abcdbbefg'

3. decode()

   b y t e s . d e c o d e ( e n c o d i n g = ′ U T F − 8 ′ , e r r o r s = ′ s t r i c t ′ ) bytes.decode(encoding='UTF-8',errors='strict') bytes.decode(encoding=UTF8,errors=strict)
  对字符串按指定编码进行解码。正好与 e n c o d e ( ) encode() encode()是相反的操作。

	str_x = 'abcdbbefg'
    print(str_x.encode(encoding='utf-8'))
    print(str_x.encode(encoding='utf-8').decode(encoding='utf-8'))
>>>>b'abcdbbefg'
>>>>abcdbbefg

4. format()

   s t r . f o r m a t ( ∗ a r g s , ∗ ∗ k w a r g s ) str.format(*args, **kwargs) str.format(args,kwargs)
  执行字符串格式化操作。
  格式: { < 参 数 序 号 > : \{<参数序号>: {<>: < 格 式 控 制 符 > } <格式控制符>\} <>}。其中,格式控制标记用来控制参数显示时的格式,格式内容如下:

:<填充><对齐><宽度>< , >< .精度 ><类型>
引导符号用于填充的单个字符,默认为空格< 左对齐 > 右对齐 ^ 居中输出宽度数字的千位分隔符,适用于整数和浮点数浮点数小数部分的精度或字符串的最大输出长度整数类型,b,c,d,o,x,X,浮点数类型,e,E,f,%
	print("{:*^15}".format('abcdefg'))
    print("{:,}".format(1000000))
    print("{:.2f}".format(3.1415926))
    print("{:.2%}".format(3.1415926))
    print("{:.2e}".format(1000000))
    print("{:b}".format(5))
>>>>****abcdefg****
>>>>1,000,000
>>>>3.14
>>>>314.16%
>>>>1.00e+06
>>>>101

  * a r g s args args表示任何多个无名参数,它是一个 t u p l e tuple tuple;** k w a r g s kwargs kwargs表示关键字参数,它是一个 d i c t dict dict。如果同时使用* a r g s args args和** k w a r g s kwargs kwargs时,* v a r g s vargs vargs参数列必须要放在** k w a r g s kwargs kwargs前面。

5. find()

   s t r . f i n d ( s u b [ , s t a r t [ , e n d ] ] ) str.find(sub[, start[, end]]) str.find(sub[,start[,end]])
  查找子字符串 s u b sub sub s t r [ s t a r t : e n d ] str[start:end] str[start:end]切片内的最小索引。如果 s u b sub sub未被找到则返回 -1,可选参数 s t a r t start start e n d end end是切片表示法。

	str_x = 'abcdbbefg'
    print(str_x.find('b'))
>>>>1

6. index()

   s t r . i n d e x ( s u b [ , s t a r t [ , e n d ] ] ) str.index(sub[, start[, end]]) str.index(sub[,start[,end]])
  类似于 f i n d ( ) find() find(),但在找不到子类时会引发 V a l u e E r r o r ValueError ValueError

7. endswith()

   s t r . e n d s w i t h ( s u f f i x [ , s t a r t [ , e n d ] ] ) str.endswith(suffix[, start[, end]]) str.endswith(suffix[,start[,end]])
  如果字符串以指定的 s u f f i x suffix suffix结尾,则返回 T r u e True True,否则返回 F a l s e False False s u f f i x suffix suffix也可以为由多个供查找的后缀构成的元组。可选参数 s t a r t start start e n d end end是切片表示法。

	str_x = 'abcdbbefg'
    print(str_x.endswith(('a', 'g')))
>>>>True

8. startswith()

   s t r . s t a r t s w i t h ( p r e f i x [ , s t a r t [ , e n d ] ] ) str.startswith(prefix[, start[, end]]) str.startswith(prefix[,start[,end]])
  如果字符串以指定的 s u f f i x suffix suffix开始,则返回 T r u e True True,否则返回 F a l s e False False。用法同 e n d s w i t h ( ) endswith() endswith()

9. join()

   s t r . j o i n ( i t e r a b l e ) str.join(iterable) str.join(iterable)
  将序列中的元素以指定的字符连接,生成一个新的字符串。

	str_list = ['abd', 'cca', 'befg']
    print(' '.join(str_list))
>>>>abd cca befg

10. split()

   s t r . s p l i t ( s e p = N o n e , m a x s p l i t = − 1 ) str.split(sep=None, maxsplit=-1) str.split(sep=None,maxsplit=1)
  以 s e p sep sep(默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等)进行分割字符串,返回分割后的列表, m a x s p l i t maxsplit maxsplit是分割次数,如果未指定,则进行所有可能的拆分。

	str_x = 'this is a pig'
    print(str_x.split())
    print(str_x.split(' '))
    print(str_x.split(' ', maxsplit=1))
>>>>['this', 'is', 'a', 'pig']
>>>>['this', 'is', 'a', 'pig']
>>>>['this', 'is a pig']

11. strip()

   s t r . s t r i p ( [ c h a r s ] ) str.strip([chars]) str.strip([chars])
  移除其中的首部和末尾字符。 c h a r s chars chars参数为指定要移除字符的字符串,默认移除空格符。 实际上 c h a r s chars chars参数并非指定单个前缀或后缀,而是会移除参数值的所有组合。

	str_x = ' this is a pig*  '
    print(str_x.strip())
    print(str_x.strip('*'))
    print(str_x.strip('* '))
>>>>this is a pig*
>>>> this is a pig*  
>>>>this is a pig

12. replace()

   s t r . r e p l a c e ( o l d , n e w [ , c o u n t ] ) str.replace(old, new[, count]) str.replace(old,new[,count])
  将所有子字符串 o l d old old替换为 n e w new new。 如果给出了可选参数 c o u n t count count,则只替换前 c o u n t count count次出现的。

	str_x = 'this is a pig'
    print(str_x.replace('pig', 'tiger'))
>>>>this is a tiger

13. zfill()

   s t r . z f i l l ( w i d t h ) str.zfill(width) str.zfill(width)
  在字符串左边填充 ′ 0 ′ '0' 0使其长度变为 w i d t h width width。 正负值前缀 ( ′ + ′ , ′ − ′ ) ('+','-') (+,)的处理方式是在正负符号之后填充而非在之前。 如果 w i d t h width width小于等于 l e n ( s t r ) len(str) len(str),则返回原字符串的副本。

	str_x = '999'
    print(str_x.zfill(5))
>>>>00999

14. lower()

   s t r . l o w e r ( ) str.lower() str.lower()
  将大写的字符均转换为小写。

	str_x = 'this Is A pig'
    print(str_x.lower())
>>>>this is a pig

15. upper()

   s t r . u p p e r ( ) str.upper() str.upper()
  将小写的字符均转换为大写。

	str_x = 'this Is A pig'
    print(str_x.upper())
>>>>THIS IS A PIG

16. capitalize()

   s t r . c a p i t a l i z e ( ) str.capitalize() str.capitalize()
  返回原字符串的副本,其首个字符大写,其余为小写。

	str_x = 'abcdefg'
    print(str_x.capitalize())
    print(str_x)
    
>>>>Abcdefg
>>>>abcdefg

17. isalnum()

   s t r . i s a l n u m ( ) str.isalnum() str.isalnum()
  检测字符串是否只由字母或数字组成,如果是返回 T r u e True True,否则返回 F a l s e False False

	str_x = 'abc9befg'
    print(str_x.isalnum())
    str_x = 'abc9 befg'
    print(str_x.isalnum())
>>>>True
>>>>False

18. isalpha()

   s t r . i s a l p h a ( ) str.isalpha() str.isalpha()
  检测字符串是否只由字母或中文组成,如果是返回 T r u e True True,否则返回 F a l s e False False

	str_x = 'abc9befg'
    print(str_x.isalpha())
    str_x = 'abc李befg'
    print(str_x.isalpha())
>>>>False
>>>>True

19. isdigit()

   s t r . i s d i g i t ( ) str.isdigit() str.isdigit()
  检测字符串是否只由数字组成,如果是返回 T r u e True True,否则返回 F a l s e False False

	str_x = 'abc9befg'
    print(str_x.isdigit())
>>>>False

20. islower()

   s t r . i s l o w e r ( ) str.islower() str.islower()
  检测字符串所有字母是否都为小写,如果是返回 T r u e True True,否则返回 F a l s e False False

	str_x = 'abc3456befg'
    print(str_x.islower())
>>>>True

21. isupper()

   s t r . i s u p p e r ( ) str.isupper() str.isupper()
  检测字符串所有的字母是否都为大写,如果是返回 T r u e True True,否则返回 F a l s e False False

	str_x = 'Abd Dihgj'
    print(str_x.isupper())
>>>>False

22. isspace()

   s t r . i s s p a c e ( ) str.isspace() str.isspace()
  检测字符串是否只由空格组成,如果是返回 T r u e True True,否则返回 F a l s e False False

	str_x = 'abc34 56befg'
    print(str_x.isspace())
>>>>False

23. istitle()

   s t r . i s t i t l e ( ) str.istitle() str.istitle()
  检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写,如果是返回 T r u e True True,否则返回 F a l s e False False

	str_x = 'Abd Dihgj'
    print(str_x.istitle())
>>>>True

24. title()

   s t r . t i t l e ( ) str.title() str.title()
  返回原字符串的标题版本,其中每个单词第一个字母为大写,其余字母为小写。

	str_x = 'this is a pig'
    print(str_x.title())
>>>>This Is A Pig

25. swapcase()

   s t r . s w a p c a s e ( ) str.swapcase() str.swapcase()
  将大写字母转换为小写,小写字母转换为大写。

	str_x = 'this iS a Pig'
    print(str_x.swapcase())
>>>>THIS Is A pIG

26. splitlines()

   s t r . s p l i t l i n e s ( [ k e e p e n d s ] ) str.splitlines([keepends]) str.splitlines([keepends])
  按照行( ′ ' \r ′ ' , ′ ' \r\n ′ ' , ′ ' \n ′ ' )分隔,返回一个包含各行作为元素的列表,如果参数 k e e p e n d s keepends keepends F a l s e False False,不包含换行符,如果为 T r u e True True,则保留换行符。

27. maketrans()

   s t r . m a k e t r a n s ( x [ , y [ , z ] ] ) str.maketrans(x[, y[, z]]) str.maketrans(x[,y[,z]])
  用于创建字符映射的转换表,如果只有一个参数 x x x,则 x x x必须是字典;如果有两个参数 x x x y y y x x x y y y必须是长度相等的字符串;如果有三个参数 x x x y y y z z z,第三个参数 z z z必须是字符串,其字符将被映射为 N o n e None None,即删除该字符。

28. translate()

   s t r . t r a n s l a t e ( t a b l e ) str.translate(table) str.translate(table)
  根据参数 t a b l e table table给出的映射表(通过 m a k e t r a n s ( ) maketrans() maketrans()方法转换而来)。

	str_x = 'this iS a Pig'
    table = str_x.maketrans('abcdefg', '1234567')
    result = str_x.translate(table)
    print(result)
>>>>this iS 1 Pi7

29. center()

   s t r . c e n t e r ( w i d t h [ , f i l l c h a r = N o n e ] ) str.center(width[, fillchar=None]) str.center(width[,fillchar=None])
  返回长度为 w i d t h width width的字符串,原字符串在其正中。 使用指定的 f i l l c h a r fillchar fillchar填充两边的空位(默认使用空格填充)。 如果 w i d t h width width小于等于 l e n ( s t r ) len(str) len(str)则返回原字符串的副本。

30. ljust()

   s t r . l j u s t ( w i d t h [ , f i l l c h a r ] ) str.ljust(width[, fillchar]) str.ljust(width[,fillchar])
  返回长度为 w i d t h width width的字符串,原字符串在其中靠左对齐。 使用指定的 f i l l c h a r fillchar fillchar填充空位 (默认使用空格)。

31. rjust()

   s t r . r j u s t ( w i d t h [ , f i l l c h a r ] ) str.rjust(width[, fillchar]) str.rjust(width[,fillchar])
  返回长度为 w i d t h width width的字符串,原字符串在其中靠右对齐。 使用指定的 f i l l c h a r fillchar fillchar填充空位 (默认使用空格)。

	str_x = 'abcdefg'
    print(str_x.center(15, '*'))
    print(str_x.ljust(15, '*'))
    print(str_x.rjust(15, '*'))
>>>>****abcdefg****
>>>>abcdefg********
>>>>********abcdefg

32. lstrip()

   s t r . l s t r i p ( [ c h a r s ] ) str.lstrip([chars]) str.lstrip([chars])
  删除字符串左边的空格或指定字符。参数同 s t r i p ( ) strip() strip()

33. rstrip()

   s t r . r s t r i p ( [ c h a r s ] ) str.rstrip([chars]) str.rstrip([chars])
  删除字符串右边的空格或指定字符。参数同 s t r i p ( ) strip() strip()

34. rfind()

   s t r . r f i n d ( s u b [ , s t a r t [ , e n d ] ] ) str.rfind(sub[, start[, end]]) str.rfind(sub[,start[,end]])
  从右边开始查找,参数同 f i n d ( ) find() find()

35. rindex()

   s t r . r i n d e x ( s u b [ , s t a r t [ , e n d ] ] ) str.rindex(sub[, start[, end]]) str.rindex(sub[,start[,end]])
  从右边开始查找,参数同 i n d e x ( ) index() index()

36. rsplit()

   s t r . r s p l i t ( s e p = N o n e , m a x s p l i t = − 1 ) str.rsplit(sep=None, maxsplit=-1) str.rsplit(sep=None,maxsplit=1)
  从右边开始分割,参数同 s p l i t ( ) split() split()

  更多方法请参考官方文档

结束语

  根据平时使用的情况,做一个简单小结:平时做爬虫,不同的文本需要进行不同的编/解码,常用 e n c o d e ( ) encode() encode() d e c o d e ( ) decode() decode()方法,如果涉及到加/解密还会用到其他函数库,这些以后在Python爬虫专栏里面说;对字符进行格式化输出、分割、替换字符等等,常用 f o r m a t ( ) format() format() s p l i t ( ) split() split() r e p l a c e ( ) replace() replace()等;将列表转换成字符串,常用 j o i n ( ) join() join();对文本进行预处理,删除换行符时,常用 s t r i p ( ) strip() strip();给文件统一按序号命名,常用 z f i l l ( ) zfill() zfill();其他对字符串的处理,比如匹配,常用的是正则表达式,即 r e re re库。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏小悠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值