Python字符串方法详解

字符串是 Python 中最常用的数据类型。我们可以使用引号('或")来创建字符串。

创建字符串很简单,只要为变量分配一个值即可。例如:

var1 = 'Hello World!'
  • 1
  • 2

python中字符串对象提供了很多方法来操作字符串,功能相当丰富。

print(dir(str))

[…‘capitalize’, ‘casefold’, ‘center’, ‘count’, ‘encode’, ‘endswith’, ‘expandtabs’, ‘find’, ‘format’, ‘format_map’, ‘index’, ‘isalnum’, ‘isalpha’, ‘isdecimal’, ‘isdigit’, ‘isidentifier’, ‘islower’, ‘isnumeric’, ‘isprintable’, ‘isspace’, ‘istitle’, ‘isupper’, ‘join’, ‘ljust’, ‘lower’, ‘lstrip’, ‘maketrans’, ‘partition’, ‘replace’, ‘rfind’, ‘rindex’, ‘rjust’, ‘rpartition’, ‘rsplit’, ‘rstrip’, ‘split’, ‘splitlines’, ‘startswith’, ‘strip’, ‘swapcase’, ‘title’, ‘translate’, ‘upper’, ‘zfill’]

  • 1
  • 2
  • 3
  • 4

注意,python中字符串是不可变对象,所以所有修改和生成字符串的操作的实现方法都是另一个内存片段中新生成一个字符串对象。例如,‘abc’.upper()将会在划分另一个内存片段,并将返回的ABC保存在此内存中。
下文出现的"S"表示待操作的字符串。本文没有对casefold,encode,format,format_map进行介绍,前两者和unicode有关,后两者内容有点太多。
1.大小写转换

1.1 lower、upper

S.lower()
S.upper()

 
 
  • 1
  • 2

返回S字符串的小写、大写格式。(注意,这是新生成的字符串,在另一片内存片段中,后文将不再解释这种行为)
例如:

>>> print('ab XY'.lower())
ab xy
>>> print('ab XY'.upper())
AB XY

 
 
  • 1
  • 2
  • 3
  • 4

1.2 title、capitalize

S.title()
S.capitalize()

 
 
  • 1
  • 2

前者返回S字符串中所有单词首字母大写且其他字母小写的格式,后者返回首字母大写、其他字母全部小写的新字符串。
例如:

>>> print('ab XY'.title())
Ab Xy
>>> print('abc DE'.capitalize())
Abc de

 
 
  • 1
  • 2
  • 3
  • 4

1.3 swapcase

S.swapcase()

 
 
  • 1

swapcase()对S中的所有字符串做大小写转换(大写–>小写,小写–>大写)。

>>> print('abc XYZ'.swapcase())
ABC xyz

 
 
  • 1
  • 2

2.isXXX判断

2.1 isalpha,isdecimal,isdigit,isnumeric,isalnum

S.isdecimal()
S.isdigit()
S.isnumeric()
S.isalpha()
S.isalnum()

 
 
  • 1
  • 2
  • 3
  • 4
  • 5

测试字符串S是否是数字、字母、字母或数字。对于非Unicode字符串,前3个方法是等价的。
例如:

>>> print('34'.isdigit())
True
>>> print('abc'.isalpha())
True
>>> print('a34'.isalnum())
True

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

2.2 islower,isupper,istitle

S.islower()
S.isupper()
S.istitle()

 
 
  • 1
  • 2
  • 3

判断是否小写、大写、首字母大写。要求S中至少要包含一个字符串字符,否则直接返回False。例如不能是纯数字。
注意,istitle()判断时会对每个单词的首字母边界判断。例如,word1 Word2、word1_Word2、word1()Word2中都包含两个单词,它们的首字母都是"w"和"W"。因此,如果用istitle()去判断它们,将返回False,因为w是小写。
例如:

>>> print('a34'.islower())
True
>>> print('AB'.isupper())
True
>>> print('Aa'.isupper())
False
>>> print('Aa Bc'.istitle())
True
>>> print('Aa_Bc'.istitle())
True
>>> print('Aa bc'.istitle())
False
>>> print('Aa_bc'.istitle())
False

下面的返回False,因为非首字母C不是小写

>>> print(‘Aa BC’.istitle())
False

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

2.3 isspace,isprintable,isidentifier

S.isspace()
S.isprintable()
S.isidentifier()

 
 
  • 1
  • 2
  • 3

分别判断字符串是否是空白(空格、制表符、换行符等)字符、是否是可打印字符(例如制表符、换行符就不是可打印字符,但空格是)、是否满足标识符定义规则。
例如:
判断是否为空白。没有任何字符是不算是空白。

>>> print(' '.isspace())
True
>>> print(' \t'.isspace())
True
>>> print('\n'.isspace())
True
>>> print(''.isspace())
False
>>> print('Aa BC'.isspace())
False

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

判断是否是可打印字符。

>>> print('\n'.isprintable())
False
>>> print('\t'.isprintable())
False
>>> print('acd'.isprintable())
True
>>> print(' '.isprintable())
True
>>> print(''.isprintable())
True

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

判断是否满足标识符定义规则。
标识符定义规则为:只能是字母或下划线开头、不能包含除数字、字母和下划线以外的任意字符。

>>> print('abc'.isidentifier())
True
>>> print('2abc'.isidentifier())
False
>>> print('abc2'.isidentifier())
True
>>> print('_abc2'.isidentifier())
True
>>> print('_abc_2'.isidentifier())
True
>>> print('_Abc_2'.isidentifier())
True
>>> print('Abc_2'.isidentifier())
True

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3.填充

3.1 center

S.center(width[, fillchar])

 
 
  • 1

将字符串居中,左右两边使用fillchar进行填充,使得整个字符串的长度为width。fillchar默认为空格。如果width小于字符串的长度,则无法填充直接返回字符串本身(不会创建新字符串对象)。
例如:
使用下划线填充并居中字符串

>>> print('ab'.center(4,'_'))
_ab_
>>> print('ab'.center(5,'_'))
__ab_

 
 
  • 1
  • 2
  • 3
  • 4

使用默认的空格填充并居中字符串

>>> print('ab'.center(4))
 ab 
>>> print(len('ab'.center(4)))
  • 1
  • 2
  • 3
  • 4

width小于字符串长度

>>> print('abcde'.center(3))
abcde

 
 
  • 1
  • 2

3.2 ljust和rjust

S.ljust(width[, fillchar])
S.rjust(width[, fillchar])

 
 
  • 1
  • 2

ljust()使用fillchar填充在字符串S的右边,使得整体长度为width。rjust()则是填充在左边。如果不指定fillchar,则默认使用空格填充。
如果width小于或等于字符串S的长度,则无法填充,直接返回字符串S(不会创建新字符串对象)。
例如:

>>> print('xyz'.ljust(5,'_'))
xyz__
>>> print('xyz'.rjust(5,'_'))
__xyz

 
 
  • 1
  • 2
  • 3
  • 4

3.3 zfill

S.zfill(width)

 
 
  • 1

用0填充在字符串S的左边使其长度为width。如果S前右正负号+/-,则0填充在这两个符号的后面,且符号也算入长度。
如果width小于或等于S的长度,则无法填充,直接返回S本身(不会创建新字符串对象)。

>>> print('abc'.zfill(5))
00abc

>>> print(’-abc’.zfill(5))
-0abc

>>> print(’+abc’.zfill(5))
+0abc

>>> print(‘42’.zfill(5))
00042

>>> print(’-42’.zfill(5))
-0042

>>> print(’+42’.zfill(5))
+0042

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

其余用例,本文不再详说,参考网址:
https://www.jb51.net/article/141376.htm

        </div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值