python基礎之資料類型

來源:互聯網
上載者:User

標籤:模型   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基礎之資料類型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.