python字符串一些函数

另一篇python字符串格式转换(补充版)

到达文章底部

1.创建字符串

s1="Welcom to China"
s2=str("Welcom to China")
s3="Welcom to China"
print(s1==s2)   #True
print(id(s1)==id(s2))#True
print(id(s1)==id(s3)) #True
print(id(s2)==id(s3)) #True

2.处理字符串的函数 len max min replace

s="welcome"
print(len(s))#7
print(max(s))#w
print(min(s))#c

如果想将字符串中的某个子串替换为指定的字符串,可以调用方法replace。
该方法的第1个参数指定被替换的子串,第2个参数指定替换子串的字符串。
该方法返回替换后得到的字符串,替换前的字符串不发生变化。
调用该方法时可以通过第3个参数指定最大替换次数。


In [1]: s = 'hello world!hello girls and boys!'

In [2]: s.replace('hello','hi',2)
Out[2]: 'hi world!hi girls and boys!'

In [3]: s.replace('hello','hi',3)
Out[3]: 'hi world!hi girls and boys!'

In [4]: s.replace('hello','hi',1)
Out[4]: 'hi world!hello girls and boys!'

3 [start:end] 截取start->(end-1) 遵循切片

s="welcome"
print(s[:]) #welcome
print(s[:7]) #welcome
print(s[:6]) #welcom
print(s[0:-1]) #welcom,负数表示(负数+长度)
print(s[2,1]) #IndentationError: unexpected indent

4 * 和+

s="nihao"
s=s*2
print(s)#nihaonihao
s=s+"你好啊"
print(s)#nihaonihao你好啊

5 in 和not in

s="hello"
print('a' in s) #False

6 比较字符 > < == != <= >=

# print("Red"<"red") #True

7 迭代字符串

s="helloworld"
for i in s:
    print(i,end=" ")
print()
for i in range(len(s)):
    print(s[i],end=" ")

8 is开头的字符串函数

isalnum()
如果字符串至少有一个字符并且所有字符都是字母或数字则返 回 True,否则返回 False
isalpha()
如果字符串至少有一个字符并且所有字符都是字母则返回 True, 否则返回 False
isdigit()
如果字符串只包含数字则返回 True 否则返回 False
islower()
如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False
isnumeric()
如果字符串中只包含数字字符,则返回 True,否则返回 False
isdecimal:判断指定字符串是否全部由十进制的数字组成。
isspace()
如果字符串中只包含空白,则返回 True,否则返回 False.
istitle()
如果字符串是标题化的(见 title())则返回 True,否则返回 False
isupper()
如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False

In [1]: print('abc'.isalpha()) # 都是字母
   ...: print('abc1'.isalpha())
True
False

In [2]: print('123'.isdecimal()) # 全部由十进制
   ...: print('123VI'.isdecimal())
   ...:
True
False

In [3]: print('1237VI'.isnumeric()) #  不支持罗马数字
   ...: print('1237Vla'.isnumeric()) 
   ...: print('1237一'.isnumeric())
False
False
True

In [4]: print('abc123六VI'.isalnum())
    ...: print('abc123六VI!'.isalnum())
    ...:
True
False

isidentifier:判断指定的字符串是否是合法的标识符。
可以调用模块keyword中的方法iskeyword判断一个字符串是否是关键字。

In [1]: 'abc'.isidentifier()
Out[1]: True

In [2]: 'def'.isidentifier()
Out[2]: True

In [3]: 'math'.isidentifier()
Out[3]: True

In [4]: '9a'.isidentifier()
Out[4]: False

In [5]: import keyword

In [6]: keyword.iskeyword('if')
Out[6]: True

In [7]: keyword.iskeyword('Main')
Out[7]: False

In [8]: keyword.iskeyword('main')
Out[8]: False

In [9]: '\t \r \n \b'.isspace()
Out[9]: False

In [10]: '\t \r \n'.isspace()
Out[10]: True

9 删除前、后导字符串

lstrip()
截掉字符串左边的空格或指定字符串。
rstrip()
删除字符串字符串末尾的空格或指定字符串
strip([chars])
在字符串上执行 lstrip()和 rstrip(),该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。

s="helloworld,orange,ok,oh,my god"
s1=s.lstrip("e")
print(s1)   #helloworld,orange,ok,oh,my god

s2=s.lstrip("h")
print(s2) 	#elloworld,orange,ok,oh,my god

s3=s.strip("d")
print(s3)	 #helloworld,orange,ok,oh,my go

s4=s.rstrip("d")
print(s4) 	# helloworld,orange,ok,oh,my go

调用以上三个方法时可以指定一个字符串,这样,
前导字符串指的是:从左边第1个字符开始依次往后,直到某个字符不在指定的字符串中。
后续字符串指的是:从右边最后1个字符开始依次往前,直到某个字符不再指定的字符串中。

在这里插入图片描述

In [1]: s = 'www.example.com'

In [2]: s.lstrip('cmowz.')
Out[2]: 'example.com'

In [3]: s.rstrip('cmowz.')
Out[3]: 'www.example'

In [4]: s.strip('cmowz.')
Out[4]: 'example'

10 字符串对齐

如果想要设置字符串的对齐方式,可以调用字符串的以下方法:
1.center:中心对齐
2.ljust:左对齐
3.rjust:右对齐
这三个方法都可以接收两个参数,其中,
第1个参数指定字符串的宽度。如果指定的宽度小于等于字符串的长度,返回字符串本身。
第2个参数指定填充字符,且第2个参数是可选的,其默认值是空格
4.zfill:右对齐,左边用0填充
该方法只接收一个参数,用于指定字符串的宽度。如果指定的宽度小于等于字符串的长度,返回字符串本身。

In [1]: print('HelloWorld'.center(18,'*'))
   ...: print('HelloWorld'.center(18))
   ...: print('HelloWorld'.center(8))
   ...: print('HelloWorld'.ljust(18,'*'))
   ...: print('HelloWorld'.ljust(18))
   ...: print('HelloWorld'.ljust(8))
   ...: print('HelloWorld'.rjust(18,'*'))
   ...: print('HelloWorld'.rjust(18))
   ...: print('HelloWorld'.rjust(8))
   ...:
****HelloWorld****
    HelloWorld
HelloWorld
HelloWorld********
HelloWorld
HelloWorld
********HelloWorld
        HelloWorld
HelloWorld

In [2]: print('578'.zfill(6))
   ...: print('-578'.zfill(6))
   ...: print('578'.zfill(2))
   ...: print('-578'.zfill(3))
   ...:
000578
-00578
578
-578

11 格式化

Python 使用 % 格式化字符串,常用占位符如下表所示:
%s 格式化字符串
%d或%i 格式化整数
%f 格式化浮点数

方式一 : % 作为占位符

 print("hello %s"%"python") #hello python
 print("hello %d"%123) #hello 123
 print("hello %f"%1.23)#hello 1.230000
 
# 1.当定义的格式化字符串中包含两个及两个以上的占位符时,必须将所有的实际值封装在元组中。
print( "%f , %s " % (1.23, "书") ) # '1.230000 , 书 '

# 2.如果不确定使用哪种占位符,那么%S永远起作用,它会把任何数据类型转换为字符串
print("%s" % 123) # '123'

# 3.如果定义的格式化字符串中的%是一个普通字符,需要使用%%对其进行转义。
print( "%.2f%%" % 80 ) # '80.00%'

# 占位符%中可以指定宽度。
# 数字和字符串都是右对齐。
# 占位符%中可以指定精度

方式二 : ‘{}’.format

# 1. 如果占位符{}中不指定参数,方法format的参数会按顺序依次匹配所有的占位符[}。
print('{} {}'.format('Hello', 'Python')) # Hello Python

# 2.占位符{}中可以指定位置参数,0表示方法format的第1个参数,1表示方法format的第2个参数
print('{0} {1}'.format('Hello', 'Python')) # Hello Python

# 3.可以在方法format中指定关键字参数的名称和值,在占位符{}中指定关键字参数的名称。
print('花了{p},买了一本书:{b},只花了{p}!'.format(p=20.5,b='python入门'))
# 花了20.5,买了一本书:python入门,只花了20.5!

# 4.占位符{}中可以使用冒号指定整数的表示形式。其中,位置参数或关键字参数的名称放在冒号前面。
In [1]: print('{:d}'.format(58))
58

In [2]: print('{:b}'.format(58))
111010

In [3]: print('{:x}'.format(58))
3a

In [4]: print('{:X}'.format(58))
3A

In [5]: print('{:f}'.format(58))
58.000000

In [6]: print('{:o}'.format(58))
72

In [7]: #使用逗号作为千位分隔符
    ...: print('{:,}'.format(12345678))#12,345,678
12,345,678

In [8]: print('{0:b}'.format(58))
111010

In [9]: print('{num:b}'.format(num=58))
111010
 
 # 5. 占位符{}中还可以使用冒号指定宽度。其中,数字是右对齐,字符串是左对齐

# 6. 占位符{}中还可以使用冒号指定宽度和精度。
In [1]: from math import pi
In [2]: print('{:8.3f}'.format(pi))
   3.142

# 7. 占位符{}中还可以使用冒号指定其它格式。
from datetime import datetime

In [1]: print("{:%Y-%m-%d %H:%M:%S}".format(datetime.now()))
2022-08-11 22:18:58

# 8. 还可以调用内置函数format得到格式化字符串,它与字符串的方法format:是等价的:'{:m}'.format(n)等价于:format(n,'m')
In [1]: '{:8.3f}'.format(pi)
Out[1]: '   3.142'

In [2]: format(pi, '8.3f')
Out[2]: '   3.142'

方式三:$作为占位符

# 可以导入模块string中的类Template:并使用美元符作为占位符,从而得到格式化字符串。
In [1]:from string import Template
    ...: price = 68.88
    ...: book="《数据结构与算法》"
    ...: tmpl = Template('花了$p,买了一本书:$b')
    ...: #1. 调用方法substitute
    ...: print(tmpl.substitute(p=price, b=book))
    ...: print(tmpl.safe_substitute({'p':price,'b':book}))
    ...: #当占位符没有匹配的实际值时,抛出KeyError。
    ...: print(tmpl.substitute(p=price))
    ...:
花了68.88,买了一本书:《数据结构与算法》
花了68.88,买了一本书:《数据结构与算法》
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-43-2ed8cec286d6> in <module>()
      7 print(tmpl.safe_substitute({'p':price,'b':book}))
      8 #当占位符没有匹配的实际值时,抛出KeyError。
----> 9 print(tmpl.substitute(p=price))

D:\Anaconda\envs\python3_6\lib\string.py in substitute(*args, **kws)
    124             raise ValueError('Unrecognized named group in pattern',
    125                              self.pattern)
--> 126         return self.pattern.sub(convert, self.template)
    127
    128     def safe_substitute(*args, **kws):

D:\Anaconda\envs\python3_6\lib\string.py in convert(mo)
    117             named = mo.group('named') or mo.group('braced')
    118             if named is not None:
--> 119                 return str(mapping[named])
    120             if mo.group('escaped') is not None:
    121                 return self.delimiter

KeyError: 'b'

# 2. 调用方法safe_substitute 由方法名可知,safe_substitute比substitute.更安全,当占位符没有匹配的实际值时,并不会抛出KeyError,而是使用占位符本身作为其实际值。
In [1]: print(tmpl.safe_substitute(p=price))
花了68.88,买了一本书:$b

12 多行字符串

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

13 字符串拼接

在这里插入图片描述

14 字符串查找 index和find区别

在这里插入图片描述

在这里插入图片描述

15 特色字符

在这里插入图片描述

16 原始字符串

在这里插入图片描述

17 字符串排序

与列表不同的是,字符串是不可变类型,因此,如果想对字符串中的所有字符进行排序,不存在方法sort,只能调用内置函数sorted。

In [1]: string = "DBeFac"

In [2]: sorted(string)
Out[2]: ['B', 'D', 'F', 'a', 'c', 'e']

In [3]: sorted(string,reverse=True)
Out[3]: ['e', 'c', 'a', 'F', 'D', 'B']

调用内置函数sorted时,还可以指定参数key=函数名或key=类名.方法名,
这样会对字符串中的所有字符分别调用指定的函数或方法,然后按照函数或方法的返回值进行排序,
从而自定义排序规则侧。

In [4]: sorted(string,key=str.lower)
Out[4]: ['a', 'B', 'c', 'D', 'e', 'F']

In [5]: sorted(string,key=str.upper)
Out[5]: ['a', 'B', 'c', 'D', 'e', 'F']


在这里插入图片描述

18 字符串大小写转换的一些函数

如果想要对字符串中某些字符的大小写进行转换,可以调用字符串的以下方法:
upper:把所有字符全部转换为大写。
Lower:把所有字符全部转换为小写。
swapcase:把所有小写字符转换为大写,把所有大写字符转换为小写。
capitalize:把第一个字符转换为大写,把其余字符转换为小写。
title:把每个单词的第一个字符转换为大写,把每个单词的剩余字符转换为小写。
casefold() 方法是Python3.3版本之后引入的,其效果和 lower() 方法非常相似,都可以转换字符串中所有大写字符为小写。
两者的区别是:lower() 方法只对ASCII编码,也就是‘A-Z’有效,对于其他语言(非汉语或英文)中把大写转换为小写的情况只能用 casefold() 方法。

In [1]: s = 'hello world Python'

In [2]: s.upper()
Out[2]: 'HELLO WORLD PYTHON'

In [3]: s.lower()
Out[3]: 'hello world python'

In [4]: s.swapcase()
Out[4]: 'HELLO WORLD pYTHON'

In [5]: s.capitalize()
Out[5]: 'Hello world python'

In [6]: s.title()
Out[6]: 'Hello World Python'

如果想要判断字符串中某些字符的大小写,可以调用字符串的以下方法:
isupper 是否所有字符全为大写。
islower 是否所有字符全为小写。
istitle 是否每个单词的第一个字符为大写并且每个单词的剩余字符为小写。
更多查看本文的 8 is开头的字符串函数

19 字符串的多个字符对应转换 maketrans

如果想对字符串中的某些字符进行转换,可以调用方法maketrans.和translate。
首先调用方法maketranst创建一个转换表,然后把创建的转换表作为参数传给方法translate。

创建三种形式

In [1]: table = str.maketrans("bcd","234")

In [2]: table
Out[2]: {98: 50, 99: 51, 100: 52}

In [3]: table = str.maketrans({"b":"2", "c":"3","d":"4"})

In [4]: table
Out[4]: {98: '2', 99: '3', 100: '4'}

In [5]: table = str.maketrans({98: 50, 99: 51, 100: 52})

In [6]: table
Out[6]: {98: 50, 99: 51, 100: 52}

转换案例

In [7]: s = "incredible"

In [8]: s.translate(table)
Out[8]: 'in3re4i2le'

增加删除字符


In [9]: table = str.maketrans("bcd","234", "ie")

In [10]: table
Out[10]: {98: 50, 99: 51, 100: 52, 101: None, 105: None}

In [11]: s.translate(table) #  s = "incredible"
Out[11]: 'n3r42l'

只删除不替换

In [13]: table = str.maketrans("","", "ie")

In [14]: table
Out[14]: {101: None, 105: None}

In [15]: s.translate(table)
Out[15]: 'ncrdbl'

API详情:

In [16]: help(str.maketrans)
Help on built-in function maketrans:

maketrans(x, y=None, z=None, /)
    Return a translation table usable for str.translate().

    If there is only one argument, it must be a dictionary mapping Unicode
    ordinals (integers) or characters to Unicode ordinals, strings or None.
    Character keys will be then converted to ordinals.
    If there are two arguments, they must be strings of equal length, and
    in the resulting dictionary, each character in x will be mapped to the
    character at the same position in y. If there is a third argument, it
    must be a string, whose characters will be mapped to None in the result.

20 字符串的分割和合并 split rsplit partition rpartition join

20.1 分割

一、调用方法split或rsplit劈分字符串
方法split从字符串的左边开始劈分,方法rsplit从字符串的右边开始劈分。
默认的劈分符是空格字符串。这两个方法的返回值都是一个列表。

可以通过参数sep指定劈分字符串时的劈分符。

可以通过参数maxsplit指定劈分字符串时的最大劈分次数,在经过最大次劈分后,剩余的子串
会单独作为一部分。此时,方法split和rsplit就有区别了。

In [1]: s = 'Python Swift Kotlin java'

In [2]: s.split(maxsplit=2,sep=' ')
Out[2]: ['Python', 'Swift', 'Kotlin java']

In [3]: s.rsplit(maxsplit=2,sep=' ')
Out[3]: ['Python Swift', 'Kotlin', 'java']

二、调用方法partition或rpartition劈分字符串
方法partition,从字符串的左边开始劈分,方法rpartition从字符串的右边开始劈分。
调用这两个方法时都必须指定劈分符。
方法partition在指定的劈分符第一次出现的地方(方法rpartition在指定的劈分符最后一次
出现的地方),将字符串劈分为三部分:
1.劈分符前面的部分
2.劈分符
3.劈分符后面的部分
这两个方法的返回值都是一个元组。
如果字符串中不存在指定的劈分符,方法partition返回的元组中的三个元素依次为:
1.字符串本身
2.空字符串
3.空字符串
如果字符串中不存在指定的劈分符,方法rpartition返回的元组中的三个元素依次为:
1.空字符串
2.空字符串
3.字符串本身

In [1]: s = 'Python Swift Kotlin java'

In [2]: s.partition(' ')
Out[2]: ('Python', ' ', 'Swift Kotlin java')

In [3]: s.rpartition(' ')
Out[3]: ('Python Swift Kotlin', ' ', 'java')

In [4]: s.partition('|')
Out[4]: ('Python Swift Kotlin java', '', '')

In [5]: s.rpartition('|')
Out[5]: ('', '', 'Python Swift Kotlin java')

20.2 合并

In [1]: #字符串之间用''进行分隔
   ...: s ='|'.join(['Python','Swift','Kotlin']);print(s)
   ...: s ='|'.join(('Python','Swift','Kotlin'));print(s)
Python|Swift|Kotlin
Python|Swift|Kotlin

In [2]: s =''.join(['Python','Swift','Kotlin']);print(s)
PythonSwiftKotlin

In [3]: #可以把字符串看做是字符的列表
   ...: s ='|'.join('Python');print(s)
P|y|t|h|o|n

回到文章头部

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

广大菜鸟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值