0
点赞
收藏
分享

微信扫一扫

python基础3---循环和字符串列表

沐之轻语 2022-02-26 阅读 80
python

复习:

今日内容:

循环:

for循环

基本的for循环语法:

代码运行:

# 定义字符串
my_str = 'hello'
# 遍历字符串,字符串中有5个字符,循环会执行5次
for i in my_str:   # 每次循环i 的值为字符串中的字符
    print('我错了', i)

结果:

for做指定次数的循环 :

range()变形

代码示例:

break和continue

代码示例:

# 获取输入的字符串
result = input('请输入一个字符串:')
# 遍历打印这个字符串
for i in result:
    # 在遍历的时候,如果这个字符是e,不打印(即后续的代码不执行)
    if i == 'e':
        continue   # 本次循环后续的代码不执⾏,执⾏下⼀次循环
    print(i)
print('-' * 30)
for i in result:
    # 3.在遍历的时候,如果这个字符是e,不打印(即后续的代码不执⾏)
    #如果这个字符不是e,打印
    if i != 'e':
        print(i)

容器

字符串

定义:

代码示例:

# 使⽤单引号定义
my_str1 = 'hello'
print(my_str1, type(my_str1))
# 使用双引号定义
my_str2 = "hello"
print(my_str2, type(my_str2))
# 使用定义三引号
my_str3 = '''hello'''
print(my_str3, type(my_str3))
# 4.字符串本身包含引号I'm⼩明
# 4.1字符串本身包含单引号,则在定义的时候不能使⽤单引号
# .2字符串本身包含双引号,则在定义的时候不能使⽤双引号
my_str4 = "I'm 小米"
print(my_str4)
# 5.字符串本身包含单引号,在定义的时候,我就是想使⽤单引号
# 5.1 使⽤\转义字符,将字符串本身的引号进⾏转义\' -->'     \" --> "
my_str5 = 'I\'m 小米'
print(my_str5)
# 5.2
# 字符串本身包含 I\'m⼩明  ,使用\\ --> \
my_str6 = 'I\\\'m 小米'
print(my_str6)
# 5.3 在字符串前边加上r""原⽣字符串,字符串中的\不会作为转义字符,⽂件操作会⽤⼀下
my_str7 = r'I\'m 小米'
print(my_str7)
my_str8 = r'I\\\'m 小米'
print(my_str8)

下标:

代码示例:

str1 = 'abcdefg'
# 1.打印字符串中最开始位置的字符
print(str1[0])
# 2.打印最后⼀个位置的数据
print(str1[-1])
# 3.打印倒数第⼆个位置的字符
print(str1[-2])
# 打印下标为2的数据
print(str1[2])
# 获取字符串中字符的个数(获取字符串的⻓度)
# len(字符串)
# length(⻓度)
num = len(str1)
print(num)
#⻓度-1的下标位置是最后⼀个字符
print(str1[num-1])   # g最后⼀个字符,倒数第⼀个
print(str1[len(str1)-1])   # g最后⼀个字符,倒数第⼀个



切片:

代码示例:

# 切⽚会得到⼀个字符串,即可以获取字符串中的多个字符
str1 = 'abcdefg'
# 1.获取abc字符
print(str1[0:3:1])
# 1.1 如果步⻓是1可以不写,最后⼀个冒号也不写
print(str1[0:3])
# 1.2 如果开始位置为0 ,可以不写,但是冒号必须有
print(str1[:3])
# 2.获取efg字符
print(str1[4:7])
print(str1[-3:7])
# 2.1如果最后⼀个字符也要取,可以不写,但是冒号必须有
print(str1[4:])
# 2.2如果开始和结束都不写,获取全部内容,但是冒号必须有
print(str1[:])
# 3.获取aceg   # 0 2 4 6,所以步⻓为2
print(str1[0:7:2])
print(str1[::2])
# 4.特殊应⽤,步⻓为负数,开始和结束不写,意思全变,⼀般不⽤管,只有⼀种使⽤场景
#反转(逆置)字符串   字符串[::-1]
print(str1[::-1])

字符串的查找⽅法find

代码示例:

str1 = 'and itcast and itheima and Python'
# 在字符串中查找and
num = str1.find('and')
print(num)
# 在字符串中查找第⼆个and出现的下标,从第⼀次出现的后⼀位开始找
num1 = str1.find('and',num+1)
print(num1)
# 在字符串中查找第三个and出现的下标,从第二次出现的后⼀位开始找
num2 = str1.find('and',num1+1)
print(num2)
# 在字符串中查找第四个and出现的下标,从第三次出现的后⼀位开始找
num3 = str1.find('and',num2+1)
print(num3)

字符串的替换⽅法replace

代码示例:

str1 = ' good good study '
# 1,将str1中所有的g改为G
str2 = str1.replace('g','G')
print('str1:', str1)  # str1:  good good study
print('str2:', str2)  # str2:  Good Good study
# 1,将str1中第一个的good改为GOOD
str3 = str1.replace('good','GOOD',1)
print('str3:', str3)  # str3:  GOOD good study
# 1,将str1中第e二个的good改为GOOD
# 3.1先将全部的good--> GOOD
str4 = str1.replace('good','GOOD')
# 3.2 再将第⼀个GOOD --> good
str5 = str4.replace('GOOD','good',1)
print('str5:', str5)  # str5:  good GOOD study

字符串的拆分split

代码示例;

str1 = "hello world and itcast and itheima and Python"
# 1.将str1按照and字符进⾏拆分
result = str1.split('and')
print(result)  # ['hello world ', ' itcast ', ' itheima ', ' Python']
# 2,将str1按照and字符进⾏拆分,拆分⼀次
result2 = str1.split('and',1)
print(result2)  # ['hello world ', ' itcast and itheima and Python']
# 3.按照空⽩字符进⾏切割
result3 = str1.split()
print(result3)   # ['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']
# 4.按照空⽩字符进⾏切割,拆分⼀次
result4 = str1.split(maxsplit=1)
print(result4)  # ['hello', 'world and itcast and itheima and Python']

字符串的链接join

代码示例:

list1 = ['good', 'good', 'study']
# 1.将列表中的字符串使⽤空格连起来
str1 = ' '.join(list1)
print(str1)  # good good study
# 2.将列表中的字符串使⽤and连起来
str2 = ' and '.join(list1)
print(str2)  # good and good and study

列表

定义;

# 定义空列表(没有任何数据的列表)
# 变量 = list()
list1 = list()
print(type(list1), list1)  # <class 'list'> []
# 类型转换list(容器)将其他的容器转换为列表
# 转换字符串会将字符串中的每个字符作为⼀个数据存⼊到列表中
list2 = list('hello')
print(type(list2), list2)  # <class 'list'> ['h', 'e', 'l', 'l', 'o']
# 直接使⽤[]进⾏定义(常⽤)
# 1定义空列表
my_list = []
print(my_list)  # []
# 2、定义非空列表
my_list1 = [1,'小米',3.14,False]
print(my_list1)  # [1, '小米', 3.14, False]

列表⽀持下标和切⽚,⻓度

代码示例:

list1 = ['小米',18,1.74,True]
# 获取第⼀个数据,名字
print(list1[0])
# 获取最后⼀个数据
print(list1[-1])
# 第⼀第⼆个数据
print(list1[0:2])
# 列表⻚⽀持len()求⻓度的,求数据元素个数
print(len(list1))

查找-查找列表中数据下标的⽅法

查找-判断是否存在

查找-统计出现的次数

代码示例;

my_list = [1,3,5,7,2,3]
num = my_list.index(3)
print(num)
# 找数据4出现的下标
# num1 = my_list.index(4)  代码会报错
# print(num1)
if 4 in my_list:
    num1 = my_list.index(4)
    print(num1)
else:
    print('不存在数据4')
# my_list.count(4)统计数据4出现的次数
if my_list.count(4) > 0:
    num2 = my_list.index(4)
    print(num2)
else:
    print('不存在数据4')

添加数据的⽅法

尾部添加(最常⽤)

指定下标位置添加

列表合并

代码示例:

my_list = []
print(my_list)
# 1.列表中添加数据郭德纲
my_list.append('郭德纲')
print(my_list)
# 2.向列表的尾部添加郭麒麟
my_list.append('郭麒麟')
print(my_list)
# 3.在下标位置为1的位置添加数据岳岳
my_list.insert(1,'岳岳')
print(my_list)
# 4.在下标位置为1的位置添加数据于谦
my_list.insert(1,'于谦')
print(my_list)
list1 =['孙越','烧饼']
# 将list1中数据逐个添加到my_list中
my_list.extend(list1)
print(my_list)
# 将list1作为⼀个整体添加到my_list
my_list.append(list1)
print(my_list)


举报

相关推荐

0 条评论