標籤:模型 cap size int style font lov star 字串
1.資料類型
python中的資料類型
python使用物件模型來儲存資料,每一個資料類型都有一個內建的類,每建立一個資料,實際就是在初始化產生一個對象,即所有資料都是對象。
2.字串
2.1定義
定義:它是一個有序的字元的集合,用於儲存和表示基本的文本資訊,‘’或“”或‘’‘ ’‘’中間包含的內容稱之為字串
特性:
1.只能存放一個值
2.不可變
3.按照從左至右的順序定義字元集合,下標從0開始順序訪問,有序
2.2字串常用操作
msg=‘hello‘
移除空白 msg.strip()
分割 msg.split(‘|‘)
長度 len(msg)
索引 msg[3] msg[-1]
切片 msg[0:5:2] #0 2 4
字元工廠函數str()
#首字母大寫
# x=‘hello‘
# print(x.capitalize())
#首字母大寫
# x=‘hello‘
# print(x.title())
#所有字母大寫
# x=‘hello‘
# print(x.upper())
# 置中顯示
# x=‘hello‘
# print(x.center(30,‘#‘))
#統計某個字元的出現的次數,空格也算字元
# x=‘hel lo love‘
# print(x.count(‘l‘))
# print(x.count(‘l‘,0,4)) # 0 1 2 3
# x=‘hello ‘
# print(x.endswith(‘ ‘)) #取x的後面幾位字元
# print(x.startswith()) #取x的前面幾位字元
#查看字元中單字元的位置
# x=‘hello ‘
# print(x.find(‘e‘))
# print(x.find(‘l‘))
#格式化字串
# msg=‘Name:{},age:{},sex:{}‘
# print(msg) #Name:{},age:{},sex:{}
# print(msg.format(‘egon‘,18,‘male‘))
python基礎之資料類型