Python基础之字符串

字符串内置处理函数

1.capitalize()

描述:

将字符串的第一个字母变成大写,其他字母变小写。

示例:

a= "hello world"
print (a.capitalize()) # 将“hello world”字符串开头字母变成大写

输出结果:

2. center()

描述:返回一个原字符串居中,并使用空格填充至长度 width 的新字符串。默认填充字符为空格。

示例:

1 a= "hello world"
2 print (a.center(30,'*')) #将“hello world”居中,并用“*”填充,占据30个字符串的长度

 输出结果:

3.count()

描述:用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。

sub -- 搜索的子字符串

start -- 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。

end -- 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。

示例:

a="hello world"
print(a.count("l")) #统计子字符串“l”在字符串“hello world”出现了多少次
print(a.count("l",4)) #统计子字符串“l”在字符串“helloworld”出现了多少次,从第四个索引位置开始,如果不指定结束索引位置,默认则到最后一个字符结束
print(a.count("l",1,3)) #统计子字符串“l”在字符串“hello world”出现了多少次,从第一个索引位置开始,到第三个索引位置结束(虽然第三个也是“l”,但python只会找到第二个索引位置,顾头不顾尾)
print(a.count("l",1,-1)) #统计子字符串“l”在字符串“hello world”出现了多少次,从第一个索引位置开始,到倒数第一个(最后一个)索引位置结束;
”-1“代表倒数第一个索引位置,”-2“代表倒数第二个索引位置,以此类推

输出结果:

 4.endswitch

描述:

用于判断字符串是否以指定后缀(元素)结尾,如果以指定后缀(元素)结尾返回True,否则返回False。可选参数"start"与"end"为检索字符串的开始与结束位置。

语法:

str.endswith(suffix[, start[, end]])

参数:

suffix -- 该参数可以是一个字符串或者是一个元素。

start -- 字符串中的开始位置。

end -- 字符中结束位置。

返回值:

如果检测到是以指定的字符串结尾则返回True,否则返回False。

示例:

a= "hello world"
print(a.endswith("l")) #判断字符串是否以“l”结尾
str1="this is string example....wow!!!";
suffix = "wow!!!";
print (str1.endswith(suffix));  #判断str1是否以“wow!!!”结尾
print (str1.endswith(suffix,20)); #判断str1是否以“wow!!!”结尾,从第20个索引位置开始,到最后一个子字符串结束
suffix = "is";
print (str1.endswith(suffix, 2, 4)); #判断str1是否以“wow!!!”结尾,从第2个索引开始,到第4个结束
print (str1.endswith(suffix, 2, 6)); #判断str1是否以“wow!!!”结尾,从第2个索引开始,到第6个结束,虽然第6个是“s”,但是python只会找到第5个“i”,顾头不顾尾

输出结果:

5.startswitch

描述:用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。

str -- 检测的字符串。

strbeg -- 可选参数用于设置字符串检测的起始位置。

strend -- 可选参数用于设置字符串检测的结束位置。

判断字符串是不是以h开头,不是则为False,是为True

示例:

1 msg="hello  world"
2 print(msg.startswith('h'))

输出结果:

 6.find

描述:查找“g”在字符串当中的位置,如果存在返回索引位置,不存在则返回-1,如果存在多个,则只会返回第一个索引位置,不会报错

示例:

1 msg='hello world'
2 print(msg.find('r'))
3 print(msg.find('g'))

输出结果:

7.index

描述:在已知道sub(子字符串,也就是“l”)存在的情况下,查找"g"在字符串当中的位置,返回它的索引,如果不存在就报错

存在的情况:

1 msg='hello world'
2 print(msg.index('l'))

 

不存在的情况:

示例:

1 msg='hello world'
2 print(msg.index('x'))

输出结果:

 注意:index与find的区别是:index已经知道msg中存在sub,然后进行查找,如果不存在会报错;find是去查找sub,有则返回索引,没有则返回-1,不会报错。

8.isdigit

描述:判断字符串是否为纯数字,是则返回True,否则返回Flase

msg='12312321312313'
print(msg.isdigit())

注意:必须字符串内全部都是数字(纯数字),包含数字也会返回Flase,无论是数字开头还是数字结尾都会返回Flase

示例:

1 msg1='hello worrld123'
2 msg2='1231asdf'
3 print(msg1.isdigit())
4 print(msg2.isdigit())

输出结果:

9.join()

描述:用于将序列中的元素以指定的字符连接生成一个新的字符串,多用于字符串拼接

示例1:

msg='hello world'
msg_new='*'.join(msg)
print(msg_new)

输出结果:

示例2:

1 str = "-";
2 seq = ("a", "b", "c"); # 字符串序列
3 print (str.join( seq ));

输出结果:

 

10.split()

描述:通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串

示例:

msg='root:x:0:0:root:/root:/bin/bash'
print(msg.split(':'))
print(msg.split(':',maxsplit=1))

输出结果:

示例2:

msg='root:x:0:0:root:/root:/bin/bash'
msg_list=msg.split(':')
print(msg_list)
print(''.join(msg_list))
print('abc'.join(msg_list))

输出结果:

11.upper()

描述:将字符串中的小写字母转为大写字母。

示例:

msg='hellH world'
print(msg.upper())

输出结果:

12.swapcase

描述:将字符串当中的大写转换小写,小写转换成大写(用于对字符串的大小写字母进行转换。)

示例:

msg='HellO world'
print(msg.swapcase())

输出结果: 

 

13.strip()

描述:用于移除字符串头尾指定的字符(默认为空格)

示例:

msg='               hello world                        '
print(msg)
print(msg.strip())#去掉字符串开头和结尾的空格,不指定的话默认是空格
msg='*********abc********'
print(msg.strip('*'))#去掉字符串开头和结尾的“*”
msg='***********he*llo wor*ld*******************'
print(msg.strip())#只会去掉字符串开头和结尾的"*",字符串中间的不会去掉

  

 输出结果:

14.lstrip()

描述:用于截掉字符串左边的空格或指定字符

msg='               hello world                        '
print(msg)
print(msg.lstrip())#去掉字符串左边的空格,不指定的话默认是空格
msg='*********abc********'
print(msg.lstrip('*'))#去掉字符串左边的“*”
msg='***********he*llo wor*ld*******************'
print(msg.lstrip('*'))#只会去掉字符串左边的"*",字符串中间的不会去掉

输出结果:

15.rstrip()

描述:用于截掉字符串右边的空格或指定字符

示例:

msg='               hello world                        '
print(msg)
print(msg.rstrip())#去掉字符串右边的空格,不指定的话默认是空格
msg='*********abc********'
print(msg.rstrip('*'))#去掉字符串右边的“*”
msg='***********he*llo wor*ld*******************'
print(msg.rstrip('*'))#只会去掉字符串右边的"*",字符串中间的不会去掉

 输出结果:

16.replace()

描述:把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次,不指定则全部替换。

示例:

msg='hello***world'
print(msg.replace('*','',1))#将字符串中的“*”替换成空,只替换1次

msg='hello***world'
print(msg.replace('*','',)) #将字符串中的“*”替换成空,没有指定替换几次则全部替换

msg='hello egon'
print(msg.replace('egon','xxx'))
#将字符串当中的“egon”替换成“xxx”,没有指定替换几次则全部替换,“egon”只有一个所以只替换一次

输出结果:

不常用的方法

msg='hello world'
print(msg.isalpha())#msg是纯字母返回True,不是则返回False
print(msg.isidentifier())#msg是内置标识符,返回True,否则返回False
print(msg.isspace())#msg是空格,返回True,反之,返回False
print(msg.istitle())#msg是标题,也就是首字母大写,返回True
print(msg.ljust(10))#10个字符左对齐
print(msg.ljust(10,'*'))#10个字符左对齐,10个字符*填充
print(msg.rjust(10))#10个字符右对齐
print(msg.rjust(10,'*'))#10个字符右对齐,10个字符*填充
print(msg.zfill(20))#总长度20个,不足则在右边添加0
 
message='''aaa
bbb
ccc
ddd
'''
print(message.splitlines()) #按照行数切分

  

字符串索引操作

msg='hello'
#字符串索引操作
print(msg[4])
print(msg[-2])
#字符串的切分操作
print(msg[0:3]) #切分原则:顾头不顾尾
print(msg[0:])
print(msg[:3])
print(msg[0:2000:2])#按两个字符切分
print(msg[::-1])#hello倒过来

 

变量解压操作

msg='hello'
x,y,z,*_=msg
print(x)
print(y)
print(z)
x,y,z='abc','aaa','xxx'
print(x)
print(y)
print(z)

  

原文地址:https://www.cnblogs.com/hui520/p/6260567.html