-
str
-
索引切片步长
s1 = 'python全栈22期' # 对字符串进行索引,切片出来的数据都是字符串类型。 # 按照索引取值 # 从左至右有顺序,下标,索引。 s2 = s1[0] print(s2,type(s2)) s3 = s1[2] print(s3) s4 = s1[-1] print(s4) # 按照切片取值。 # 顾头不顾腚 s5 = s1[0:6] s5 = s1[:6] print(s5) s6 = s1[6:] print(s6) # 切片步长 s7 = s1[:5:2] print(s7) print(s1[:]) # 倒序: s8 = s1[-1:-6:-1] print(s8) # 按索引:s1[index] # 按照切片: s1[start_index: end_index+1] # 按照切片步长: s1[start_index: end_index+1:2] # 反向按照切片步长: s1[start_index: end_index后延一位:2] # 思考题:倒序全部取出来?
-
练习题
2.有字符串s = "123a4b5c" 通过对s切片形成新的字符串s1,s1 = "123" 通过对s切片形成新的字符串s2,s2 = "a4b" 通过对s切片形成新的字符串s3,s3 = "1345" 通过对s切片形成字符串s4,s4 = "2ab" 通过对s切片形成字符串s5,s5 = "c" 通过对s切片形成字符串s6,s6 = "ba2"
-
常用操作方法
# upper lower # s1 = s.upper() # # s1 = s.lower() # print(s1,type(s1)) # 应用: username = input('用户名') password = input('密码') code = 'QweA' print(code) your_code = input('请输入验证码:不区分大小写') if your_code.upper() == code.upper(): if username == '太白' and password == '123': print('登录成功') else: print('用户名密码错误') else: print('验证码错误')
-