字符串操作
1.in 和 not in
字符串1 in 字符串2 - 判断字符串1是否是字符串2的子串(判断字符串2中是否包含字符串1)
print(‘a’ in ‘abc’) # True
print(‘ab’ in ‘abc’) # True
print(‘ac’ in ‘abc’) # False
2.相关函数
len、str
1)求长度
str1 = ‘曾经有一段真挚的爱情…’
print(len(str1))
str2 = ‘\tabc\n123’
print(len(str2)) # 8
str3 = r’\tabc\n123’
print(len(str3)) # 10
str4 = ‘abc 123’
print(len(str4)) # 8
str5 = ’ abc123’
print(len(str5)) # 10
2)str(数据) - 将数据准换成字符串
a.哪些能转:所有的数据都可以转换成字符串
b.怎么转:在数据打印值外面加引号
num = 100
print(str(num)) # ‘100’
print(str(True)) # ‘True’
print(str([1, 2, 3])) # ‘[1, 2, 3]’
c.字符串转数字
str6 = ‘782’
print(int(str6))
去掉引号后本身是整数的字符串才能转换成整型
str7 = ‘12.5’
print(int(str7))
str8 = ‘12.5’
print(float(str8)) # 12.5
print(float(str6)) # 782.0
str9 = ‘3e4’
print(float(str9)) # 30000.0
s3 = {10, [1, 2]}
print(s3)
格式化字符串
name = input(‘请输入姓名:’)
name = ‘小明’
age = int(input(‘请输入年龄:’))
age = 23
money = float(input(‘请输入月收入:’))
money = 78273.982
message: xxx今年xx岁