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

Python-语法学习-{循环,字符串}

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

Python-语法学习-{循环,字符串}

1*6 6 2*6 12 3*6 18 4*6 24 5*6 30 6*6 36 1*7 7 2*7 14 3*7 21 4*7 28 5*7 35 6*7 42 7*7 49 1*8 8 2*8 16 3*8 24 4*8 32 5*8 40 6*8 48 7*8 56 8*8 64 1*9 9 2*9 18 3*9 27 4*9 36 5*9 45 6*9 54 7*9 63 8*9 72 9*9 81 for循环

语法

for 临时变量 in 序列:
for else

str1 asdasd 
for i in str1:
 print(i,end )
else:
 print()
 print( The code after loop ends normally )

output

asdasd
The code after loop ends normally
字符串 切片

切⽚是指对操作的对象截取其中⼀部分的操作。字符串、列表、元组都⽀持切⽚操作。
语法

name abcdefg 
print(name[2:5:1]) # cde
print(name[2:5]) # cde
print(name[:5]) # abcde
print(name[1:]) # bcdefg
print(name[:]) # abcdefg
print(name[::2]) # aceg
print(name[:-1]) # abcdef, 负1表示倒数第⼀个数据
print(name[-4:-1]) # def
print(name[::-1]) # gfedcba
查找

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


基本一模一样

修改 replace()

mystr hello world and itcast and itheima and Python 
# 结果 hello world he itcast he itheima he Python
print(mystr.replace( and , he ))
# 结果 hello world he itcast he itheima he Python
print(mystr.replace( and , he , 10))
# 结果 hello world and itcast and itheima and Python
print(mystr)
split()

mystr hello world and itcast and itheima and Python 
# 结果 [ hello world , itcast , itheima , Python ]
print(mystr.split( and ))
# 结果 [ hello world , itcast , itheima and Python ]
print(mystr.split( and , 2))
# 结果 [ hello , world , and , itcast , and , itheima , and , Python ]
print(mystr.split( ))
# 结果 [ hello , world , and itcast and itheima and Python ]
print(mystr.split( , 2))

分割完的结果是list

join()

str1 What a wonderful day! 
str1.split( )
print(str1)
str1.join( )
print(str1)
capitalize()

title()

lower() 大写转小写

upper() 小写转大写

ljust() rjust() center()


判断 startswitch()

endswitch()

isalpha

isdigit()

isalnum()

isspace()

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

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

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