2.3. 修改切片方向
print(s1[1:5:1]) # 輸出 ello 默認從1開始 差值為1
print(s1[1:5:2]) # 輸出 el 默認從1開始 差值為2
print(s1[-1:-5:-1]) # 反方向的種 從後面開始 差值1 索引到4
print(s1[:]) # 不寫數字就默認都要
print(s1[2:]) # 從索引2開始往後都要
print(s1[:5]) # 從索引0開始往後要到4
print(s1[::2]) # 索引所有值 差值2
2.4.統計字元串中字元的個數
print(len(s1)) # 12 總共有12個數
2.5.移除字元串首尾指定的字元
username = input('username>>>:').strip()
# username = input('username>>>:')
# username = username.strip() 兩行程式碼和上面一樣程式碼運行結果一樣 單程式碼更方便!
if username == 'wei': # 如果後面沒有strip(),'wei ' 如果有空字元串,輸出有誤!
print('登陸成功')
res = ' jason '
print(len(res)) # 空字元串也佔位1 長度9個字元
print(len(res.strip())) # strip()括弧內不寫 默認移除首尾的空格 結果:5
res1 = '$$wei$$'
print(res1.strip('$')) # wei
print(res1.lstrip('$')) # wei$$
print(res1.rstrip('$')) # $$wei
2.6.切割字元串中指定的字元
res = 'jason|123|read'
print(res.split('|')) # ['jason', '123', 'read'] 該方法的處理結果是一個列表
# split翻譯:
# V:使....分裂/n:分裂; 分離; 分歧; 劃分; 分別; 份額; 裂縫; 裂口; 劈叉
name, password, hobby = res.split('|') # 使用變數名代替對應的字元
print(res.split('|', maxsplit=1)) # maxsplit:最大切割值 後面數字多少 切割多少
# ['jason', '123|read'] 默認從左往右切指定個數
print(res.rsplit('|',maxsplit=1))
# ['jason|123', 'read'] 從右往左切指定個數
resplit:從右到左
2.7.字元串格式化輸出
format玩法1:等價於佔位符
res = 'my name is {} my age is {}'.format('wei', 123) # {}等價於佔位符
print(res) # my name is wei my age is 123
format玩法2:索引取值並支援反覆使用
res = 'my name is {0} my age is {1} {0} {0} {1}'.format('jason', 123)
print(res) # my name is jason my age is 123 jason jason 123
format玩法3:佔位符見名知意
res = 'my name is {name1} my age is {age1}'.format(name1='jason', age1=123)
print(res) # my name is jason my age is 123 jason 123 jason
format玩法4:推薦使用(******* 給雞哥拉滿!!!!!!)
name = input('username>>>:')
age = input('age>>>:')
res = f'my name is {name} my age is {age}' # f代表了 .format
print(res)
3.字元串
1.大小寫相關
res = 'hElLO WorlD 666'
print(res.upper()) # HELLO WORLD 666 全部大寫
print(res.lower()) # hello world 666 字母全部小寫
'''
圖片驗證碼:生成沒有大小寫統一的驗證碼 展示給用戶看
獲取用戶輸入的驗證碼 將用戶輸入的驗證碼和當初產生的驗證碼統一轉大寫或者小寫再比對
'''
code = 'zBcqD1'
print('請輸入圖中的驗證碼的內容',code)
cs_code = input('請輸入驗證碼>>>:').strip()
if cs_code.upper() == code.upper(): # upper:全部大寫 lower:英文小寫
print('驗證碼正確')
3.1 判斷字元串中的大小寫
res = 'hello world'
print(res.isupper()) # 判斷字元串是否是純大寫 False
print(res.islower()) # 判斷字元串是否是純小寫 True
3.2 判斷字元串是否是純數字
res = ''
print(res.isdigit()) # sdigit是python的一個函數,主要用於檢查是否為數字 False
guess_age = input('guess_age>>>:').strip() # 獲取用戶輸入值
if guess_age.isdigit(): # 判斷用戶輸入值是否是數字
guess_age = int(guess_age) # 如果是
print('年齡', guess_age) # 列印年齡
else:
print('代筆!年齡都不知道怎麼輸入啊??'))
3.3替換字元串中指定的內容
res = 'my name is jason jason jason jason jason'
print(res.replace('jason', '你想要輸入的')) # 前面不變 後面更改
# my name is handsome handsome handsome handsome handsome(輸出結果)
print(res.replace('jason', 'tonySB', 1))
# my name is tonySB jason jason jason jason 從左到右替換指定內容
3.4.字元串的拼接
ss1 = 'hello'
ss2 = 'world'
print(ss1 + '$$$' + ss2) # hello$$$world
print(ss1 * 10) # 10遍hello
print('|'.join(['jason', '123', 'read', 'JDB'])) # jason|123|read|JDB 加入到每個字元串
print('|'.join(['jason', 123])) #運行失敗 參與拼接的數據值必須都是字元串
3.5 統計指定字元出現的次數
res = 'hello world'
print(res.count('l')) # 3 count:計數
3.6 判斷字元串的開頭或者結尾
res = 'jason say hello'
res = 'jason say hello'
print(res.startswith('jason')) # True startswith 開頭
print(res.startswith('j')) # True
print(res.startswith('jas')) # True
print(res.startswith('a')) # False
print(res.startswith('son')) # False
print(res.startswith('say')) # False
print(res.endswith('o')) # True endswith 結尾
print(res.endswith('llo')) # True
print(res.endswith('hello')) # True
3.7.其他方法補充
res = 'helLO wORld hELlo worLD'
print(res.title()) # Hello World Hello World 每個英文首字母大寫
print(res.capitalize()) # Hello world hello world 第一個英文大寫
print(res.swapcase()) # HELlo WorLD HelLO WORld 相反 大寫的小寫 小寫的大寫
print(res.index('O')) # 找索引值
print(res.find('O') # 實現檢索字元串並且輸出運算值的意思
print(res.index('c')) # 找不到直接報錯
print(res.find('c')) # 找不到默認返回 找不到默認返回-1
print(res.find('LO')) # 也是類似索引值 找到下劃線地址