python中字符串常用方法的應(yīng)用

# 字符串方法:center
# 作用是:將字符串在一定的寬度區(qū)域內(nèi)居中顯示
#  這個方法和我們之前將的format 中的 ^ 一樣 

# ^
print("<" + "hello".center(30) + ">") # <            hello             >
# <      hello       >
print("<{:^30}>".format("hello"))     # <            hello             >

# 
print("<" + "hello".center(20, "*") + ">") # <*******hello********>
print("<{:*^20}>".format("hello"))         # <*******hello********>

-------------------------------------------------
在大字符串中來查找子子字符串 如果找到了,find方法就會返回子子字符串的第一個字符在大字符串中出現(xiàn)的位置 有就是索引 如果未找到,那么find方法就會返回-1
find方法有3個參數(shù) 第一個是要查找的子字符串 第二個參數(shù)是開始索引 第三個參數(shù)是結(jié)束索引  左閉右開 
# 字符串方法:find
s = "hello world"
print(s.find("world"))  # 6  w的位置索引為 6 

print(s.find("abc")) # -1   不存在返回-1 

print(s.find("o"))  # 4    返回第一個 0的索引 
print(s.find("o",6)) # 7   第二個0 出現(xiàn)在第7個位置 

print(s.find("l",5,9)) # -1
print(s.find("l",5,10)) # 9   左閉右開

-------------------------------------------------
# 字符串方法:join

# 用于連接序列中的元素,split方法的逆方法

list = ["a", "b", "c", "d", "e"]
s = '*'
print(s.join(list))  # a*b*c*d*e
print("xy".join(list)) # axybxycxydxye

# C:\abc\xyz
# /abc/xyz
# c:\usr\local\nginx\
# /usr/local/nginx/

# 這個join 在我們 linux 和windows 中很有幫助 不同的操作系統(tǒng)的路徑用python代碼如何表示: 要表示一個 linux環(huán)境的 路徑   
# 先定義一個元組 

dirs = ('','usr','local','nginx','')
linuxPath = '/'.join(dirs)
print(linuxPath)                # /usr/local/nginx/

windowPath = ('C:' + '\\'.join(dirs))
print(windowPath)               # C:\usr\local\nginx\

#numList = [1,2,3,4,5,6]   # 這個跑出異常 必須要轉(zhuǎn)化成字符串
#print("a".join(numList))

-------------------------------------------------------

# 字符串方法:split方法  拆分成列表 
s1 = "a b c d e f"
print(s1.split())           # ['a', 'b', 'c', 'd', 'e', 'f']

s2= "a*b*c*d*e"
print(s2.split("*"))         # ['a', 'b', 'c', 'd', 'e']

path = "/usr/local/nginx"
pathList = path.split('/')
print(pathList)            # ['', 'usr', 'local', 'nginx']
windowPath = "D:" + "\\".join(pathList)
print(windowPath)           # D:\usr\local\nginx

--------------------------------------------------
lower: 把所有的英文字母變成小寫
upper:把所有的英文字母變成大寫
capwords函數(shù):會將獨立的字符串中首字母大寫

# 字符串方法:lower、upper和capwords函數(shù)
print("HEllo".lower())  # hello
print("hello".upper())  # HELLO

list = ["Python", "Ruby", "Java","KOTLIN"]
if "Kotlin" in list:
    print("找到Kotlin了")
else:
    print("未找到Kotlin")   # 未找到Kotlin 對大小寫敏感

for lang in list:                    # 循環(huán)遍歷一遍 這樣把每一個單詞都轉(zhuǎn)化為小寫
    if "kotlin" == lang.lower():
        print("找到Kotlin了")       # 找到Kotlin了
        break;

from string import capwords
s = "i not only like Python, but also like Kotlin"
print(capwords(s))
# I Not Only Like Python, But Also Like Kotlin

---------------------------------------------------
replace方法:將一個字符串中出現(xiàn)的同樣的子字符串 替換,這里最常用的就是把所有的控制臺輸入的空格 替換成空。例如:
"""
IDE = input('請輸入IDE的名字')
findIDE = IDE.replace(' ', '').lower() # 把輸入的空格轉(zhuǎn)化為空 并且 把輸入的值轉(zhuǎn)化為小寫 
"""

strip方法:截取前后空格

# 字符串方法:replace和strip

s = "abcdaedf"
print(s.replace("a", "12345")) # 12345bcd12345edf

print(s.replace("xyz","aa"))  # abcdaedf   不存在的話 就返回原字符串

print("   geekori.com   ".strip())  # geekori.com
print(" <  geekori.com  >  ".strip()) # <  geekori.com  >  只會截取前后的空格,不會截取 中間的空格 

langList = ["python", "java", "ruby", "scala", "perl"]
lang = "   python   "
if lang in langList:
    print("找到了python")
else:
    print("未找到python")   # 未找到python

if lang.strip() in langList:
    print("找到了python")
else:
    print("未找到python")  # 找到了python

s = "***  $$*  Hello * World   ***$$$  "
print(s.strip(" *$"))    # Hello * World             " *$"  這里面的三個是或的關(guān)系
-------------------------------------------------

# 字符串方法:translate和maketrans

# translate:替換單個字符  maketrans方法:轉(zhuǎn)化為字典 

s = "I not only like python, but also like kotlin."
table = s.maketrans("ak", "*$")  # 他的這個意思是把a替換為*      k替換為 $
print(table)             # {97: 42, 107: 36}  先轉(zhuǎn)發(fā)為字典 ACLLZ嗎值 
print(s.translate(table)) # I not only li$e python, but *lso li$e $otlin.
table1 = s.maketrans("ak", "*$", " ")  # 第三個參數(shù) 刪除的意思   整體的意思是: 把a 轉(zhuǎn)化為 * ,k 轉(zhuǎn)化為 $  并刪除掉中間的空格 
print(table1)                       # {97: 42, 107: 36, 32: None}
print(s.translate(table1))          # Inotonlyli$epython,but*lsoli$e$otlin.
# 把所有的 空格后刪除了 

網(wǎng)站欄目:python中字符串常用方法的應(yīng)用
分享網(wǎng)址:http://bm7419.com/article48/isgjhp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、自適應(yīng)網(wǎng)站、云服務(wù)器、App開發(fā)靜態(tài)網(wǎng)站、網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司