日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 基础之字符串方法

發布時間:2023/12/18 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 基础之字符串方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

字符串

print('chenxi'*8)

  測試

D:\python\python.exe D:/untitled/dir/for.py chenxichenxichenxichenxichenxichenxichenxichenxiProcess finished with exit code 0

  字符串切片打印處理

print('chenxi'[2:])

  測試

D:\python\python.exe D:/untitled/dir/for.py enxi

  判斷字符串里有沒有包含對應得字符

print('x'in 'chenxi') print('f'in 'chenxi')

  測試

True False

  字符串拼接,不建議使用這種方法

a='cx' b='zrd' c=a+b print(c)

  測試

D:\python\python.exe D:/untitled/dir/for.py cxzrdProcess finished with exit code 0

  字符串拼接

a='cx' b='zrd' c=''.join([a,b]) print(c)

  測試

D:\python\python.exe D:/untitled/dir/for.py cxzrdProcess finished with exit code 0

  字符串拼接

a='cx' b='zrd' bb='haha' c='-----'.join([a,b,bb]) print(c)

  測試

D:\python\python.exe D:/untitled/dir/for.py cx-----zrd-----haha

  字符串之統計關鍵字個數

dis = 'hebei tianjing guanzhou' print(dis.count('i'))

  測試

D:\python\python.exe D:/untitled/dir/for.py 3

  字符串之修改首字母為大寫

dis = 'hebei tianjing guanzhou' print(dis.capitalize())

  測試

D:\python\python.exe D:/untitled/dir/for.py Hebei tianjing guanzhou

  一共打印50字符,不夠用指定字符去補,原先字符串內容居中

dis = 'hebei tianjing guanzhou' print(dis.center(50,'#'))

  測試

D:\python\python.exe D:/untitled/dir/for.py #############hebei tianjing guanzhou##############Process finished with exit code 0

  判斷字符串是不是以什么結尾的

dis = 'hebei tianjing guanzhou' print(dis.endswith('u')) print(dis.endswith('i'))

  測試

D:\python\python.exe D:/untitled/dir/for.py True FalseProcess finished with exit code 0

  判斷字符串是不是以什么開頭的

dis = 'hebei tianjing guanzhou' print(dis.startswith('he'))

  測試

D:\python\python.exe D:/untitled/dir/for.py TrueProcess finished with exit code 0

  修改字符串里tab的默認空格數量\t 表示tab鍵

dis = 'he\tbei tianjing guanzhou' print(dis.expandtabs(tabsize=10))

  測試

D:\python\python.exe D:/untitled/dir/for.py he bei tianjing guanzhouProcess finished with exit code 0

  找到字符串里第一個元素定返回索引值

dis = 'hebei tianjing guanzhou' print(dis.find('a'))

  測試

D:\python\python.exe D:/untitled/dir/for.py 8

  將字符串變量賦值打印

dis = 'hebei tianjing guanzhou {name}' print(dis.format(name='zrd'))

  測試

D:\python\python.exe D:/untitled/dir/for.py hebei tianjing guanzhou zrdProcess finished with exit code 0

  以字典方式批量給字符串賦值

dis = 'hebei tianjing guanzhou {name} ll {age}' print(dis.format_map({'name':'zrd','age':22}))

  測試

D:\python\python.exe D:/untitled/dir/for.py hebei tianjing guanzhou zrd ll 22Process finished with exit code 0

  查字符串里關鍵字并返回索引值,字符串里沒有關鍵字報錯

dis = 'hebei tianjing guanzhou {name} ll {age}' print(dis.index('i')) print(dis.index('d'))

 測試

D:\python\python.exe D:/untitled/dir/for.py Traceback (most recent call last):File "D:/untitled/dir/for.py", line 170, in <module>print(dis.index('d')) ValueError: substring not found 4

  判斷字符串是否包含特殊字符

print('test564'.isalnum() ) print('tygc*'.isalnum()) print('reswd'.isalnum()) print('7890'.isalnum()) print('宋氏家族'.isalnum())

  測試

D:\python\python.exe D:/untitled/dir/for.py True False True True True

  判斷字符串像不像十進制的數字

print("3455".isdecimal()) print("程序".isdecimal()) print("AF09".isdecimal()) print("hevb".isdecimal())

  測試

D:\python\python.exe D:/untitled/dir/for.py True False False False

  判斷字符串是否包含一個非法字符

print("dtfghvh".isidentifier()) print("1233tyghgvh".isidentifier()) #非法字符

  測試

D:\python\python.exe D:/untitled/dir/for.py True False

  判斷字符是不是空格

print(' '.isspace())

  測試

D:\python\python.exe D:/untitled/dir/for.py TrueProcess finished with exit code 0

  判斷字符串是不是標題格式

print('My Ch'.istitle()) print('My ch'.istitle())

  測試

True False

  字符串里所有大寫改成小寫

print('My Ch'.lower())

  測試

D:\python\python.exe D:/untitled/dir/for.py my chProcess finished with exit code 0

  字符串所有小寫該大寫

print('My Ch'.upper())

  測試

D:\python\python.exe D:/untitled/dir/for.py MY CHProcess finished with exit code 0

  字符串中大小寫翻轉

print('My Ch'.swapcase())

  測試

D:\python\python.exe D:/untitled/dir/for.py mY cHProcess finished with exit code 0

  在字符串后面以特定字符補夠特定數量

print('My Ch'.ljust(50,'*'))

  測試

D:\python\python.exe D:/untitled/dir/for.py My Ch*********************************************Process finished with exit code 0

  在字符串前面以特定字符補夠特定數量

print('My Ch'.rjust(50,'*'))

  測試

D:\python\python.exe D:/untitled/dir/for.py *********************************************My ChProcess finished with exit code 0

  將字符串前后空格去掉

print(' My Ch'.strip())

  測試

D:\python\python.exe D:/untitled/dir/for.py My ChProcess finished with exit code 0

  字符串內容替換

print('chenxi ffff'.replace('ffff','zrd'))

  測試

D:\python\python.exe D:/untitled/dir/for.py chenxi zrdProcess finished with exit code 0

  字符串內容替換的次數控制

print('chenxi ffff'.replace('ffff','zrd')) print('chenxi ffff tygdf'.replace('ff','zrd')) print('chenxi ffff tygdf'.replace('ffff','zrd',1))

  測試

D:\python\python.exe D:/untitled/dir/for.py chenxi zrd chenxi zrdzrd tygdf chenxi zrd tygdfProcess finished with exit code 0

  查字符串最后一個關鍵字在第幾個索引

print('chenxi cx xlc'.rfind('x'))

 測試

D:\python\python.exe D:/untitled/dir/for.py 10Process finished with exit code 0

  將字符串以空格為分隔符,分成列表

print('chenxi cx xlc'.split(' '))

  測試

D:\python\python.exe D:/untitled/dir/for.py ['chenxi', 'cx', 'xlc']Process finished with exit code 0

  

?

轉載于:https://www.cnblogs.com/rdchenxi/p/11103425.html

總結

以上是生活随笔為你收集整理的python 基础之字符串方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。