栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python之字符串

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python之字符串

目录

定义方式

角标

切片

字符串查找函数

字符串修改

字符串分隔 split

‘字符’.join(序列)

capitalize仅开头字母大写

title 单词首字母大写

ljust rjust center 凑字符串的位数   这个方向是针对原字符串的位置展示

startwith endwith 

isxxx是否都是


定义方式

单引号 双引号 三个单引号 三个双引号 三引号输出的和书写格式保持一致

str1='''nihao
ye'''
print(str1)
# nihao
# ye

角标
str1='nihaoye'
print(str1[2])
#h

切片

就是对对象进行部分截取

字符串、列表、元组都⽀持  毕竟都存在角标一说

序列[开始位置下标:结束位置下标:步⻓]

1. 不包含结束位置下标对应的数据, 正负整数均可; 包含头不包含尾

2. 步⻓是选取间隔,正负整数均可,默认步⻓为1。

str1='0123456789'
print(str1[2:]) #23456789  包含2之后的所有
print(str1[2:5]) #234   包含2不包含5之间的
print(str1[:5]) #01234 从头到不包含5之间的
print(str1[2:10:3]) # 258 包含2不包含10 之间 每选择一次就跳3步
print(str1[:]) #原样输出
print(str1[::2]) #02468
print(str1[::-1])#9876543210
print(str1[:-1])#012345678
print(str1[-4:]) #6789
print(str1[-4:-1]) #678
print(str1[-4:-1:-1])  #没结果 找不到 因为-4 -1虽然是倒着找但是是正方形截取, -1跳步 是倒着走
print(str1[-4:1:-1])  #65432  -4倒着找的 1是正着找的 之间的方向也是倒着的 -1跳步也是倒着 所以能找到 
# 负数的话就倒着选取
# 切记倒着选取的时候不是从0开始 而是-1
#有些绕  费脑子

字符串查找函数

str.find(字串,[开始的index,结束的index])  存在返回位置即角标 不存在返回-1

str.index(字串,[开始的index,结束的index]) 存在返回位置即角标 不存在报错

str.count(字串,[开始的index,结束的index]) 出现的次数 

rfind(): 和find()功能相同,但查找⽅向为右侧开始。

rindex():和index()功能相同,但查找⽅向为右侧开始。

mystr = "hello world and itcast and itheima and Python"
print(mystr.find('and')) #12
print(mystr.find('and',15,30)) #23
print(mystr.find('ands')) #-1
#print(mystr.index('ands'))  #ValueError: substring not found
print(mystr.count('and')) #3次
print(mystr.count('ands')) #0
print(mystr.count('and',15,30)) #1

字符串修改

str.replace('old','new',[次数])  吧老串换成新串 依据换几次

此函数不能修改原字符串

mystr = "hello world and itcast and itheima and Python"
print(mystr.replace('and','he'))
print(mystr.replace('and','he',2))
print(mystr)
# hello world he itcast he itheima he Python
# hello world he itcast he itheima and Python
# hello world and itcast and itheima and Python

字符串分隔 split

str.split('分隔字符',[次数]) 返回列表数组  注意该分隔字符 再字符串中必须存在

mystr = "hello world and itcast and itheima and Python"
print(mystr.split('and'))
print(mystr.split('and',2))
print(mystr.split(' '))
print(mystr.split(' ',2))
# ['hello world ', ' itcast ', ' itheima ', ' Python']
# ['hello world ', ' itcast ', ' itheima and Python']
# ['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']
# ['hello', 'world', 'and itcast and itheima and Python']

join带有角标的序列以某字符为分隔,转成字符串

‘字符’.join(序列)
mystr = "hello world and itcast and itheima and Python"
print('-'.join(mystr))
list1 = ['chuan', 'zhi', 'bo', 'ke']
print('_'.join(list1))
t1 = ('aa', 'b', 'cc', 'ddd')
print('...'.join(t1))
# h-e-l-l-o- -w-o-r-l-d- -a-n-d- -i-t-c-a-s-t- -a-n-d- -i-t-h-e-i-m-a- -a-n-d- -P-y-t-h-o-n
# chuan_zhi_bo_ke
# aa...b...cc...ddd

capitalize仅开头字母大写

整个字符串 的仅仅是第一个字母大写 ,其他转为小写

mystr = "hello world and itcast and itheima and Python"
print(mystr.capitalize())
# Hello world and itcast and itheima and python

title 单词首字母大写

每个单词的开头字母都大写,其他转为小写

mystr = "helloworld and itCast and itheima and Python"
print(mystr.title())
# Helloworld And Itcast And Itheima And Python

lower() 都转小写 upper()都转大写

strip 去除左右2边的空格

mystr = "    helloworld and itCast and itheima and Python    "
print(mystr.lstrip()) #去除左边
print(mystr.rstrip()) #去除右边
print(mystr.strip()) #都去除

ljust rjust center 凑字符串的位数   这个方向是针对原字符串的位置展示

str.ljust(要凑的位数,[凑出位数多余的用什么表示,默认是空格])

mystr = "helloworld and itCast and itheima and Python"
print(mystr.ljust(50)) #去除左边
print(mystr.rjust(50,',')) #去除右边
print(mystr.center(50,'-'))#居中
# helloworld and itCast and itheima and Python      
# ,,,,,,helloworld and itCast and itheima and Python
# ---helloworld and itCast and itheima and Python---

startwith endwith 

表示是否以某字符串开头或者结尾  返回bool

str.startwith('字符串',[startindex,endindex])

str.endwith('字符串',[startindex,endindex])

mystr = "helloworld and itCast and itheima and Python"
print(mystr.startswith('hello')) #True
print(mystr.startswith('and',11,14))#True
print(mystr.endswith('python')) #False
print(mystr.endswith('Python',38,44)) #True
print(len(mystr)) #44

isxxx是否都是

isalpha()  是否都是字母 str.isalpha()

isdigit() 是否都是数字

isalnum() 是否只包含数字或字母

isspace() 该字符串是否只有空格

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/689569.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号