1name =' aleX'
2#移除空格
print(name.strip())
3#是否al开头
print(name.startswith("al"))
4#以X结尾
print(name.endswith("X"))
5#l替换成p
print(name.replace('l','p'))
6#根据l分割
print(name.split('l'))
7#大写
print(name.upper())
8#小写
print(name.lower())
9#第二个字符
print(name[1])
10#前三个字符
print(name[:3])
11#后两个字符
print(name[3:])
12#找到e的索引值
print(name.find('e'))
13#获取子序列,去掉最后一个字符
name = "oldboy"
print(name[0:-1])
课堂总结
一.字符串的内置方法
# # 字符串的操作
# str1 = 'my name is xxx, my age is 18。'
# # 优先掌握的操作:
# # 1、按索引取值(正向取+反向取) :只能取
# print(str1[3])
#
# # 2、切片(顾头不顾尾,步长)
# print(str1[3:7]) # 3 - (7-1)
#
# # 步长为2
# print(str1[3:10:2])
#
# # 3、长度len
# print(len(str1))
# # 4、成员运算in和not in
# print('x' in str1) # True
# print('T' in str1) # False
#
# # 5、移除空白strip
# # 去除字符串左右两边的空格
# str1 = ' my name is xxx, my age is 18。 '
# print(str1)
# print(str1.strip())
#
# # 6、切分split
# # 会根据split()括号中的规则去切分字符串,然后把每一个值追加到列表中。
# str2 = 'my name is xxx, my age is 18。'
# list2 = str2.split(' ')
# print(list2)
#
# # 7、循环
# str2 = 'my name is xxx, my age is 18。'
# for line in str2:
# print(line) # print默认参数end="\n"
# print(line, end=" ") # 自定义end参数
# 需要掌握的
str4 = ' my name is xxx, my age is 18。'
str5 = 'my name is xxx, my age is 18。 '
# 1、strip,lstrip,rstrip
# l 去除左边的空格或指定符号
# r 去除右边的空格或指定符号
print(str4.lstrip())
print(str5.rstrip())
str4 = '***my name is xxx, my age is 18。'
str5 = 'my name is xxx, my age is 18。***'
print(str4.lstrip('*'))
print(str5.rstrip('*'))
# 2、lower(转换成小写),upper(转换成大写)
str6 = 'my name is Tank'
print(str6.lower())
print(str6.upper())
# 3、startswith,endswith (判断字符串首位是否成立)
print(str6.startswith("my")) # True
print(str6.startswith("MY")) # False
print(str6.endswith("Tank")) # True
print(str6.endswith("tank")) # False
# 4、format的三种玩法
# 方式一: 按照位置替换
str7 = 'my name is {}, my age is {}!'
str7 = str7.format('tank', 18, 111)
print(str7)
#
# # 方式二: 安装索引替换
str7 = 'my name is {0}, my age is {1}!'
str7 = str7.format('tank', 18, 111)
print(str7)
#
# # 方式三: 指名道姓替换
str7 = 'my name is {name}, my age is {age}!'
str7 = str7.format(age=18, name='tank')
print(str7)
# 5、split,rsplit # 从左边开始切分、从右边开始切分
# 6、join
# 字符串拼接,会对列表中的数据按照某种标识进行拼接,
# 列表中的值必须是字符串类型
name = 'tank'
age = "18"
# sex = 1
print(':'.join([name, age]))
# 报错expected str instance, int found
# print(':'.join([name, age, sex]))
# 7、replace
str8 = 'old new'
# 括号中第一个参数是旧参数,第二个参数是新参数
# 两个参数进行替换
str8 = str8.replace('old', 'python')
print(str8)
# 8、isdigit 判断字符串是否是数字
ID = input('请输入ID')
print(ID)
print(type(ID))
print(ID.isdigit())
二.列表的内置方法
list1 = ['tank', 18, 'male', 3.0, 9]
# 优先掌握的操作:
# 1、按索引存取值(正向存取+反向存取):即可存也可以取
# 正向取值
print(list1[4]) # 9
# 反向取值
print(list1[-2])
# 2、切片(顾头不顾尾,步长)
print(list1[1:5])
# 步长
print(list1[1:5:2])
# 3、长度
print(len(list1))
# 4、成员运算in和not in
print('tank' in list1) # True
print('tank' not in list1) # False
# 5、追加
# 在列表末尾追加值
list1.append('张全蛋')
list1.append('赵铁柱')
print(list1)
# 6、删除
# pop取出列表中的值
print(list1.pop()) # ()默认不写为-1,取最后一位
print(list1.pop(0)) # tank
level = list1.pop() # 取出列表中的老师等级并赋值给level变量
print(level)
print(list1) # ['tank', 18, 'male', 3.0]
# remove删除列表中的值
name = list1.remove('tank') # 直接删除列表中的tank,赋值给name为None
print(name)
print(list1) # [18, 'male', 3.0]
# 7、循环
for line in list1:
print(line)
三.基础知识
# 数字类型
# 整型int
# number = 100 # int(100)
# number2 = int(100)
# print(number)
# print(number2)
# 浮点型 float
# sal = 15.0 # float(15.0)
# sal2 = float(15.0)
# print(sal)
# print(sal2)
# 字符串类型
# name = 'tank'
# name2 = "tank"
# content = '''
# 1r1r1r
# 21r12r1
# r12r
# 12r1r21
# r12r1r2
# 12r1r
# '''
#
# print(name, name2, content)
# python中字符串可以相加、相乘
# print('tank ' * 5)
# print('tank' + 'jam')
# print('tank', 'jam')
# 列表类型
# []括号内以逗号隔开,可存放多个不同类型的值。
# list1 = ['张曼曼', 'tank', '涂懿磊', 11, 1.0, ['jason', 200]]
# print(list1[0])
# print(list1[5][0])
# 字典类型
# 在{}内,都逗号隔开,可存放多个值,每个值以key: value的形式存储
# 存
# dict1 = {'name': "tank", "age": 18} # {name="tank", "age"=18}
# 取
# print(dict1["name"])
# 布尔类型
# True or False
# print(10 == 10) # True
# print(10 == 11) # False
# 注意: 所有数据类型都自带布尔, 0、None、空都为False
# if 0:
# print('111')
#
# if None:
# print('111')
#
# if []:
# print('111')
#
# else:
# print('222')
# 格式化输出
# 可以在输出时给字符串类型做一次替换
# %s占位符,%d可以替换数字类型
# number = input('请输入:')
# str1 = '''
# 尊敬的用户,您的话费余额为%s元。
# ''' % number
#
# print(str1)
# for循环
# list1 = [1, 2, 3, 4, 5]
# for line in list1:
# print(line)
# for line in range(1, 10):
# print(line)
# dict1 = {'name': "tank", "age": 18}
# for key in dict1:
# print(key)
# print(dict1[key])