python 數據類型學習
字符串類型
split通過指定分隔符分割字符串
例子:
name = "huang zhong"
print(name.split())
strip移除字符串頭部和尾部的字符,默認空格, lstrip移除左側字符,rstrip移除右側字符
例子:
name = " alex "
print(name.strip())
capitalize首字母轉換成大寫
例子:
name = "alex"
print(name.capitalize())
casefold將所有大寫變小寫,可以轉換多門語言
例子:
name = "AleX"
v = name.casefold()
print(v)
lower將所有大寫變小寫,只能轉換英語
例子:
name = "AleX"
v = name.casefold()
print(v)
center文本居中,空白處用其他字符填充,默認空白字符填充
例子:
name = "alex"
v = name.center(20, ‘*‘)
print(v)
count子字符串在父字符串中出現的次數
例子:
name = "cndklsdnrfknsv"
num = name.count("d")
num1 = name.count("d", 5) #從第5個索引開始計算
num2 = name.count("d", 5, 9) #計算從第5到第9之間d出現的次數
print(num)
startswith是否以某個字符開始,是則返回True,否則返回False
例子:
name = "alex"
v = name.startswith("al")
print(v)
endswith是否以某個字符開始,是則返回True,否則返回False
例子:
name = "alex"
v = name.startswith("ex")
print(v)
expandtabs找到制表符並替換
例子:
name = "nsdl\tajnfcks\tsns"
v = name.expandtabs(20) #替換成20個字符,不足的補空白
print(v)
find找到子序列的索引位置,沒找到返回-1
例子:
name = "alex"
v = name.find("o") #-1
print(v)
index找到子序列的索引位置,沒找到報錯
例子:
name = "alex"
v = name.index("a")
print(v)
format字符串格式化
例子:
name = v = name.format(, , ) (v) name = v = name.format(=,=, =) (v)
isalnum是否是數字、字符、漢字
例子:
name = v = name.isalnum() (v) num = "二" v1 = num.isdecimal() # ‘123‘ v2 = num.isdigit() # ‘123‘ v3 = num.isnumeric() # ‘123‘,‘二‘
本文出自 “linux技術” 博客,請務必保留此出處http://xiaojishu.blog.51cto.com/4278020/1923283
python 數據類型學習