python字符串及其函数

python中字符串切片与字符串函数

1.字符串切片

切片格式:[起始索引:结束索引:[步长]]

注意:

  1. 步长可以省略,默认为1
  2. 包括开头不包括结尾

正向索引与逆向索引

#正向索引与逆向索引
"hello world"
h   e   l   l  o     w  o  r  l  d
0   1   2   3  4  5  6  7  8  9  10
-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
#逆向索引
s="hello world"
print(s[-1:-8:-1])
print(s[-1::-1])
print(s[:-8:-2])

正向索引与结论

s="hello world"
#全部省略即全部截取
print(s[:])
print(s[::])
#步长为1,起始索引到结束索引挨个截取,但是不包括结束索引
print(s[0:8:1])
#起始索引到结束索引挨个截取,但是不包括结束索引
print(s[0:8])
#结论:步长为1跟没有步长的效果是一样的
#步长为2,起始索引到结束索引隔一个字符取一个字符,但是不包括结束索引
print(s[0:8:2)
#起始索引到结束索引隔一个字符取一个字符,但是不包括结束索引
print(s[0:8])
#结论:步长非1的结果跟没有步长的结果是不一样的

#运行结果:
#hello world
#hello world
#hello wo
#hello wo
#********************
#hlow
#hello wo
********************

逆向索引跟正向是大同小异的就不写结论了!!!

#逆向索引
s="hello world"
print(s[-1:-8:-1])
print("*"*20)
print(s[-1::-1])
print(s[:-8:-2])
print("*"*20)

#运行结果:
#dlrow o
#********************
#dlrow olleh
#drwo
#********************

综合:

s="hello world"
print(s[-1:-8])
print(s[-2:-8:3])
print(s[-2:2:2])
#运行结果:

可以看到什么结果都没有为什么呢?
解释:
当切片步数为正时只能左边的数字小于右边的数字并在s的范围内有输出,如果不符合,输出为空或只有部分输出
当切片步数为负时只能左边的数字大于右边的数字并在s的范围内有输出,如果不符合,输出为空或只有部分输出。
字符串的查找

方法 功能
find 查找,返回从左第一个指定字符的索引,找不到返回-1
rfind 查找,返回从右第一个指定字符的索引,找不到返回-1
index 查找,返回从左第一个指定字符的索引,找不到报错
rindex 查找,返回从右第一个指定字符的索引,找不到报错
count 计数功能,返回自定字符在字符串当中的个数

代码示例:

str="--python--"
print(str.find("查找"))

print(str.rfind("p"))

print(str.index("p"))

print(str.rindex("p"))

print(str.count("-"))
#运行结果:
#-1
#2
#2
#2
#4

字符串的拆分

方法 描述
partition 把mystr以str分割成三部分,str前,str自身和str后
splitlines 按照行分隔,返回一个包含各行作为元素的列表,按照换行符分割
split 按照指定的内容进行分割,maxsplit:默认将指定的所有的内容进行分割, 可以指定 maxsplit的值,如果maxsplit=1 表示只按照第一个指定内容进行分割, 后面剩余的不分割。
str="this\nis\ngril"

print(str.splitlines())
print(type(str.splitlines()))

print(str.split("/n"))
print(type(str.split("/n")))

print(str.partition("/n"))
print(type(str.partition("/n")))
#运行结果
#['this', 'is', 'gril']
#<class 'list'>
#['this\nis\ngril']
#<class 'list'>
#('this\nis\ngril', '', '')
#<class 'tuple'>

字符串的替换

方法 描述
replace 从左到右替换指定的元素,可以指定替换的个数,默认全部替换
translate 按照对应关系来替换内容 from string import maketrans
str="this\nis\ngril"
print(str.replace("\n","-"))

#运行结果
#this-is-gril

字符串的修饰

方法 描述
center 让字符串在指定的长度居中,如果不能居中左短右长,可以指定填充内容, 默认以空格填充
ljust 让字符串在指定的长度左齐,可以指定填充内容,默认以空格填充
rjust v
zfill 将字符串填充到指定的长度,不足地方用0从左开始补充
format 按照顺序,将后面的参数传递给前面的大括号
strip 默认去除两边的空格,去除内容可以指定
rstrip 默认去除右边的空格,去除内容可以指定
lstrip 默认去除左边的空格,去除内容可以指定
str="python"
print(str.center(20,"*"))

print(str.ljust(20,"*"))

print(str.rjust(20,"*"))

print(str.zfill(20))

str="  python  "
print(str.strip())

print(str.rstrip())

print(str.lstrip())

#运行结果:
#*******python*******
#python**************
#**************python
#00000000000000python
#python
#  python
#python  

字符串格式化-百分号

格式 描述
%% %s 字
%s 字符串
%d 有符号整数(十进制)
%f 浮点数字(用小数点符号)

这里就随意写几句格式化的不同姿势把!

print("我从{}来,我到{}去,我是{}".format("天堂","地狱","小丑"))
print("我从{0}来,我到{1}去,我是{2}".format("天堂","地狱","小丑"))
print("我从{heaven}来,我到{hell}去,我是{clown}".format(heaven="天堂",hell="地狱",clown="小丑"))
print("我从%s来,我到%s去,我是%s"%("天堂","地狱","小丑"))
print("我从%(heaven)s来,我到%(hell)s去,我是%(clown)s"%{"heaven":"天堂","hell":"地狱","clown":"小丑"})
#运行结果:
#我从天堂来,我到地狱去,我是小丑
#我从天堂来,我到地狱去,我是小丑
#我从天堂来,我到地狱去,我是小丑
#我从天堂来,我到地狱去,我是小丑
#我从天堂来,我到地狱去,我是小丑

字符串变形

方法 描述
upper 将字符串当中所有的字母转换为大写
lower 将字符串当中所有的字母转换为小写
swapcase 将字符串当中所有的字母大小写互换
title 将字串符当中的单词首字母大写,单词以非字母划分
capitalize 只有字符串的首字母大写
expandtabs 把字符串中的 tab 符号(’\t’)转为空格,tab 符号(’\t’)默认的空格数是 8
str="   python  "
print(str.upper())

print(str.lower())

print(str.swapcase())

print(str.title())

print(str.capitalize())

print(str.expandtabs())
#运行结果
#PYTHON  
#   python  
#   PYTHON  
#   Python  
#   python  
#   python 

字符串判断

方法 描述
isalnum 判断字符串是否完全由字母或数字组成
isalpha 判断字符串是否完全由字母组成
isdigit 判断字符串是否完全由数字组成
isdigit 判断字符串当中的字母是否完全是大写
islower 判断字符串当中的字母是否完全是小写
istitle 判断字符串是否满足title格式
isspace 判断字符串是否完全由空格组成
startswith 判断字符串的开头字符,也可以截取判断
endswith 判断字符串的结尾字符,也可以截取判断
split 判断字符串的分隔符切片
str="python"
print(str.isdigit())

print(str.isalpha())

print(str.isalnum())

print(str.isupper())

print(str.islower())

print(str.istitle())

print(str.isspace())

print(str.startswith("p"))

print(str.endswith("n"))
#运行结果
#False
#True
#True
#False
#True
#False
#False
#True
#True

就到这把!!!

来一波,推送吧!
群号:781121386
群名:人生苦短,我学编程
欢迎大家加入我们,一起交流技术!!!

发布了38 篇原创文章 · 获赞 128 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/lujiangyang123/article/details/103394732