python中str的常用方法匯總(1)


a = 'strABC'

# Strabc  : 首字母大寫,其他全部小寫
b = a.capitalize()
print(b)

# STRABC  : 全部大寫
c = a.upper()
print(c)

# strabc  : 全部小寫
d = a.lower()
print(d)

# STRabc    大小寫翻轉
e = a.swapcase()
print(e)

# Abc Edf_Ghi*Jkl.Mno,Pqr,Stu(Vw)Xy4Z    以非英文字母隔開的首字母大寫
a = "abc edf_ghi*jkl.mno,pqr,stu(vw)xy4z"
b = a.title()
print(b)

# #######python#######     (寬度,填充物(默認空格)) 字符居中
str = "python"
str1 = str.center(20,"#")
print(str1)

# abc     def
# a       def   當字符串中出現tab時,用空格補充滿tab 8位的倍數,使得后續字符對齊
str = "abc\tdef"
str1 = "a\tdef"
str2 = str.expandtabs()
str3 = str1.expandtabs()
print(str2)
print(str3)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM