循环的作⽤:控制代码重复执⾏
while语法
while 条件:
条件成⽴重复执⾏的代码1
条件成⽴重复执⾏的代码2
......
while循环嵌套语法
while 条件1:
条件1成⽴执⾏的代码
......
while 条件2:
条件2成⽴执⾏的代码
......
for循环语法
for 临时变量 in 序列:
重复执⾏的代码1
重复执⾏的代码2
......
else
字符串
⼀对引号字符串
三引号字符串
注意:三引号形式的字符串⽀持换⾏。
字符串输出
字符串输⼊
下标
name = "abcdef"
print ( name [ 1 ])
print ( name [ 0 ])
print ( name [ 2 ])
切⽚
序列 [ 开始位置下标 : 结束位置下标 : 步⻓ ]
注意
1. 不包含结束位置下标对应的数据, 正负整数均可;
2. 步⻓是选取间隔,正负整数均可,默认步⻓为 1 。
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
注意:开始和结束位置下标可以省略,表示在整个字符串序列中查找。
mystr = "hello world and itcast and itheima and Python"
print(mystr.index('and')) # 12
print(mystr.index('and', 15, 30)) # 23
print(mystr.index('ands')) # 报错
注意:开始和结束位置下标可以省略,表示在整个字符串序列中查找。
mystr = "hello world and itcast and itheima and Python"
print(mystr.count('and')) # 3
print(mystr.count('ands')) # 0
print(mystr.count('and', 0, 20)) # 1
修改
所谓修改字符串,指的就是通过函数的形式修改字符串中的数据。
注意:替换次数如果查出⼦串出现次数,则替换次数为该⼦串出现次数。
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() :按照指定字符分割字符串。
字符串序列 . split ( 分割字符 , num )
注意: num 表示的是分割字符出现的次数,即将来返回数据个数为 num+1 个
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))
注意:如果分割字符是原有字符串中的⼦串,分割后则丢失该⼦串。
list1 = ['chuan', 'zhi', 'bo', 'ke']
t1 = ('aa', 'b', 'cc', 'ddd')
# 结果:chuan_zhi_bo_ke
print('_'.join(list1))
# 结果:aa...b...cc...ddd
print('...'.join(t1))
mystr = "hello world and itcast and itheima and Python"
# 结果:Hello world and itcast and itheima and python
print(mystr.capitalize())
注意: capitalize() 函数转换后,只字符串第⼀个字符⼤写,其他的字符全都⼩写。
title() :将字符串每个单词⾸字⺟转换成⼤写。
mystr = "hello world and itcast and itheima and Python"
# 结果:Hello World And Itcast And Itheima And Python
print(mystr.title())
mystr = "hello world and itcast and itheima and Python"
# 结果:hello world and itcast and itheima and python
print(mystr.lower())
mystr = "hello world and itcast and itheima and Python"
# 结果:HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON
print(mystr.upper())
ljust() :返回⼀个原字符串左对⻬ , 并使⽤指定字符 ( 默认空格 ) 填充⾄对应⻓度 的新字符串。
字符串序列 . ljust ( ⻓度 , 填充字符 )
判断
所谓判断即是判断真假,返回的结果是布尔型数据类型: True 或 False 。
startswith() :检查字符串是否是以指定⼦串开头,是则返回 True ,否则返回 False 。如果设置开
始和结束位置下标,则在指定范围内检查。
字符串序列 . startswith ( ⼦串 , 开始位置下标 , 结束位置下标 )
mystr = "hello world and itcast and itheima and Python "
# 结果:True
print(mystr.startswith('hello'))
# 结果False
print(mystr.startswith('hello', 5, 20))
mystr = "hello world and itcast and itheima and Python"
# 结果:True
print(mystr.endswith('Python'))
# 结果:False
print(mystr.endswith('python'))
# 结果:False
print(mystr.endswith('Python', 2, 20))
mystr1 = 'hello'
mystr2 = 'hello12345'
# 结果:True
print(mystr1.isalpha())
# 结果:False
print(mystr2.isalpha())
mystr1 = 'aaa12345'
mystr2 = '12345'
# 结果: False
print(mystr1.isdigit())
# 结果:False
print(mystr2.isdigit())
mystr1 = 'aaa12345'
mystr2 = '12345-'
# 结果:True
print(mystr1.isalnum())
# 结果:False
print(mystr2.isalnum())
mystr1 = '1 2 3 4 5'
mystr2 = ' '
# 结果:False
print(mystr1.isspace())
# 结果:True
print(mystr2.isspace())