Python的基礎資料型別 (Elementary Data Type)(1)

來源:互聯網
上載者:User

標籤:字元   必須   false   none   式表   判斷   dict   end   命令列   

  Python的資料類型

  在Python3中的基礎資料型別 (Elementary Data Type)包括:數字(int)、字串(str)、布爾值(bool)、列表(list)、元祖(tuple)、字典(dict)等,所有的資料可以通過type()函數進行查詢其資料類型。在Python的命令列鍵入資料類型(int、str、bool、list、tuple、dict等)按住Ctrl鍵同時點擊左鍵,可以查詢該資料類型的所有用法。

  ? 整形魔法(int)

  def bit_length(self):求當前字串以二進位形式表示時,至少需要用幾位表示,例如:  

1 num = 2562 n1 = num.bit_length()3 print(n1)4 9

  ? 字串魔法(str_1)

  def capitalize(self):將首字母設定為大寫形式,例如:  

1 str1 = ‘alex2 s1 = str1.ca3 print(s1)   4 Alex

  def casefold(self):將字串中的所有字母轉換成小寫形式,例如:  

1 str1 = ‘ale2 s2 = str1.c3 print(s2)  4 alex

  def lower(self):和casefold()功能一樣,不過lower()只針對英文字母有作用,對於法語、德語等其他語言的大小寫轉換如意產生錯誤,例如:  

1 str1 = ‘ale2 s2 = str1.c3 print(s2)  4 alex

  def center(self, width, fillchar=None):設定字串長度,並將字串置中顯示,fillchar是指定字串中空缺處的填儲值,預設為空白格,也可以人為設定,例如:

1 str1 = ‘alex‘2 s3 = str1.center(30, ‘#‘)3 print(s3) 4 #############alex#############
1 str1 = ‘alex‘2 s3 = str1.center(30, ‘$‘)3 print(s3) 4 $$$$$$$$$$$$$alex$$$$$$$$$$$$$

  def count(self, sub, start=None, end=None):在當前字串中差找某個子字串出現的次數,start是尋找範圍的起始位置,預設值是0,end是尋找範圍的截止為止,預設值是整個 字串,例如:

1 str2 = ‘Hello,my name is Charles Zhou!‘2 v1 = str2.count(‘a‘)                   3 print(v1)       4 2                       

  def endswith(self, suffix, start=None, end=None):判斷字串是否以某個子字串開始,返回布爾值True或者False。例如:

1 str2 = ‘Hello,my name is Charles Zhou!‘2 v2 = str2.startswith("He")             3 print(v2)                              4 True
str2 = ‘Hello,my name is Charles Zhou!‘v2 = str2.startswith("he")             print(v2)         False                     

  def startswith(self, suffix, start=None, end=None):判斷字串是否以某個子字串結束,返回布爾值True或者False。例如:

1 str2 = ‘Hello,my name is Charles Zhou!‘ 2 v3 = str2.endswith("ou!")               3 print(v3)                      4 True         
1 str2 = ‘Hello,my name is Charles Zhou!‘ 2 v3 = str2.endswith("ou")               3 print(v3)4 False                               

  def find(self, sub, start=None, end=None):在字串中尋找子字串,並返回子字串的位置,當子字串不存在時,返回-1。例如:

1 str2 = ‘Hello,my name is Charles Zhou!‘2 v4 = str2.find(‘a‘)                    3 print(v4) 4 10                             
str2 = ‘Hello,my name is Charles Zhou!‘v4 = str2.find(‘z‘)                    print(v4)                              -1

  def index(self, sub, start=None, end=None):在字串中尋找子字串,並返回子字串的位置,當子字串不存在時,代碼直接報錯。例如:

1 str2 = ‘Hello,my name is Charles Zhou!‘2 v4 = str2.index(‘n‘)                   3 print(v4)        4 9                      
1 str2 = ‘Hello,my name is Charles Zhou!‘2 v4 = str2.index(‘z‘)                   3 print(v4)                              4 ValueError: substring not found

  def format(self, *args, **kwargs):將字串中的預留位置換成指定的值。例如:

1 str3 = ‘Hello,I\‘m {name},I\‘m {age}!‘   2 v5 = str3.format(name = "Alex", age = 26)3 print(v5) 4 Hello,I‘m Alex,I‘m 26!                            
1 str4 = ‘Hello,I\‘m {0},I\‘m {1}!‘2 v5 = str4.format( "Alex",26)     3 print(v5)                        4 Hello,I‘m Alex,I‘m 26!

  值得注意的是,如果不指定名字的話,預留位置必須是0開始,不然系統會報錯,例如:

1 str4 = ‘Hello,I\‘m {1},I\‘m {2}!‘2 v5 = str4.format( "Alex",26)3 print(v5)4 5 IndexError: tuple index out of range6 Process finished with exit code 1

  def format_map(self, mapping):以索引(字典)的形式,為字串中的預留位置賦值。例如:

1 test = ‘I am {name},age {a}‘2 v4 = test.format_map({"name":‘df‘,"a":26})3 print(v4)4 I am df,age 26

  未完待續......

 

Python的基礎資料型別 (Elementary Data Type)(1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.