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让字符串在指定的长度左齐,可以指定填充内容,默认以空格填充
rjustv
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
群名:人生苦短,我学编程
欢迎大家加入我们,一起交流技术!!!

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值