目录
1、字符串的输出
# f-strings
name = "兰克奖"
age = 60
f_string1 = f'奖项:{name},年龄:{age}'
a = 10
b = 20
f_string2 = f'a + b = {a + b}'
2、字符串遍历
s = "hello"
for i in s:
print(i)
3、for-else语句
"""
格式:
for 变量 in 容器:
重复执行的代码块
else:
如果for循环不是通过break结束的时候,一定会执行的代码块
"""
s = "hello"
for i in s:
if i == "m":
print(1)
break
else:
print(2)
4、字符串切片
# 截取到最后一个元素
s = "hello"
print(s[1:])
# 从第一个元素开始截取
s = "hello"
print(s[:3])
# 步长
s = "hello python"
print(s[2:10:2])
# 步长(可以为负)
s = "hello python"
print(s[10:2:-2])
# 字符串的反转(逆序)
s = "hello python"
print(s[::-1])
5、列表
存储多个元素,元素可以是任何类型的。
6、元组
定义一个元素的元组:
tuple1 = (1,)
什么时候使用元组存储一组数据?
当那组数据不允许被修改时!