四丶人生苦短,我用python【第四篇】

來源:互聯網
上載者:User

標籤:http   資料   置中   ctrl   gets   lin   title   end   self   

1 基礎資料型別 (Elementary Data Type)
  • 數字     int
  • 字串   str
  • 布爾值   bool
  • 列表       list
  • 元組       tuple
  • 字典       dict

    》》》type()   一個參數時返回物件類型。

#!/usr/bin/env python# -*- coding:utf-8 -*-temp = "hey"lei = type(temp)print(lei)輸出:C:\Users\msi\Desktop\python\venv\Scripts\python.exe "C:/Users/msi/Desktop/python/one day.py"<type ‘str‘>Process finished with exit code 0

 註:所有數字、字串、字典等所具備的方法都存在相對應的類裡。

  》》》查看對象的類,或對象所具備的功能

    第一種:

      ctrl+滑鼠左鍵,找到對應的類,以及內部所有的方法

    第二種:dir()    快速看對象具有的功能

#!/usr/bin/env python# -*- coding:utf-8 -*-temp = "hey"lei = dir(temp)print(lei)#顯示C:\Users\msi\Desktop\python\venv\Scripts\python.exe "C:/Users/msi/Desktop/python/one day.py"[‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getnewargs__‘, ‘__getslice__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mod__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__rmod__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘_formatter_field_name_split‘, ‘_formatter_parser‘, ‘capitalize‘, ‘center‘, ‘count‘, ‘decode‘, ‘encode‘, ‘endswith‘, ‘expandtabs‘, ‘find‘, ‘format‘, ‘index‘, ‘isalnum‘, ‘isalpha‘, ‘isdigit‘, ‘islower‘, ‘isspace‘, ‘istitle‘, ‘isupper‘, ‘join‘, ‘ljust‘, ‘lower‘, ‘lstrip‘, ‘partition‘, ‘replace‘, ‘rfind‘, ‘rindex‘, ‘rjust‘, ‘rpartition‘, ‘rsplit‘, ‘rstrip‘, ‘split‘, ‘splitlines‘, ‘startswith‘, ‘strip‘, ‘swapcase‘, ‘title‘, ‘translate‘, ‘upper‘, ‘zfill‘]Process finished with exit code 0

    第三種:help()

#!/usr/bin/env python# -*- coding:utf-8 -*-temp = "hey"lei = help(type(temp))print(lei)

1.1 int 

#!/usr/bin/env python# -*- coding:utf-8 -*-# a.加法 同n1+n2n1 = 123n2 = 456print(n1.__add__(n2))#b.擷取可表示的二進位最短位元ret = n1.bit_length()print(ret)
1.2 str
#!/usr/bin/env python# -*- coding:utf-8 -*-# 1 capitalize()首字母大寫a1 = "sn"ret = a1.capitalize()print(ret)# >>>輸出Sn# 2 center(self, width, fillchar=None) 內容置中,width:總長度;fillchar:空白處填充內容,預設無rat = a1.center(10,‘*‘)print(rat)#輸出>>>    ****sn****# 3 count(self, sub, start=None, end=None)  子序列個數c1 = "sn is sn"rct = c1.count("s",0,5)     #s在大於等於0小於5的位置出現了幾次print(rct)#輸出>>> 2# 4 decode()解碼    encode()編碼# 5 endswith(self, suffix, start=None, end=None) 是否以 xxx 結束temp = "hello"print(temp.endswith(‘o‘,4,5))   #o在大於等於4小於5的位置出#輸出>>> True# 6 expandtabs(self, tabsize=None) 將tab轉換成空格,預設一個tab轉換成8個空格# 7 find(self, sub, start=None, end=None)尋找子序列位置,如果沒找到,返回 -1s = "faqfasq"print(s.find("q"))#輸出>>> 2# 8 format(*args, **kwargs) 字串格式化,動態參數p = "hello {0},age {1}"       #{0}預留位置new = p.format("sn",20)print(new)#輸出>>> hello sn,age 20
View Code

四丶人生苦短,我用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.