strpython
索引切片步長code
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('驗證碼錯誤')