# 2、字符串类型:
#写代码,有如下变量,请按照要求实现每个功能(共6分, 每小题各0.5分)
name =”aleX"
# 1) 移除name变量对应的值两边的空格,并输出处理结果
name = " aleX"name = name.lstrip()print(name)
#2) 判断name变量对应的值是否以"al” 开头,并输出结果。
name = "alex"print(name.startswith('al'))
#3) 判断name变量对应的值是否以"X”结尾,并输出结果。
name = "aleX"print(name.endswith('X'))
# 4) 将name变量对应的值中的“l” 替换为"p” ,并输出结果
name = "aleX"name = name.replace('l','p')print(name)
# 5) 将name变量对应的值根据“1” 分割,并输出结果。
name = "aleX"
print(name.split("l"))
# 6) 将name变量对应的值变大写,并输出结果。
name = "aleX"
print(name.upper())
# 7) 将name变量对应的值变小写,并输出结果●
name = "aleX"print(name.lower())
# 8) 请输出name变量对应的值的第2个字符?
name = "aleX"
print(name[1])
# 9) 请输出name 变量对应的值的前3个字符?
name = "aleX"print(name[0:3])
# 10) 请输出name变量对应的值的后2个字符?
name = "aleX"print(name[-2:])
# 11) 请输出name变量对应的值中"e" 所在索引位置?
name = "aleX" print(name.index("e"))
# 12) 获取子序列,去掉最后一个字符。如: oldboy则获取oldbo。
name12 = name[0:-1] >>>print(name12)
课堂内容:
name='tututu'print(name)name = input('input username')pwd = input('input password')print(name,pwd)print(type(name),type(pwd))#for循环list1=[1,2,3,4,5]for line in list1: print(line)for line in range(1,10): print(line)
#布尔类型print(10 == 10) # Trueprint(10 == 11) # False#默认布尔类型的值if 0: print('qwer')if None: print('qwerr')if []: print('qwer')
# 转换成小写 lower()
# 转换成小写 upper()str3 = 'My name is xxx 'print(str3.upper())# startswith ,endswith
print(str3.startswith('My'))print(str3.endswith('My'))# format三种
str7 = 'my name is {name},my age is {age}'str7 = str7.format(name='tututu', age=18)print(str7)str8 = 'my name is {1},my age is {2}'str8 = str8.format('tututu', 18, 21)print(str8)
# 列表的内置方法
# 1.按索引取值,正向从左边,负数从右边开始list = ['tututu', 18, 'male', 3.0, 9]print(list[4])print(list[-2])# 切片,顾头不顾尾,步长(x-1)
print(list[1:5])
print(list[1:5:2])# 长度len
print(len(list))# 成员运算in 和not in
print('tututu' in list)print('tututu' not in list)# 加
# 在列表末尾加值list.append('tututu')print(list)
#两种删除
# pop删除
# pop 取出列表中值,可赋值# remove
print(list.pop())
print(list)print(list.pop(0))level = list.pop()print(list)name = list.remove('tututu')print(name)print(list)