【python學習筆記01】python的資料類型

來源:互聯網
上載者:User

標籤:

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

 整型 int 

浮點型 float 

真值 bool

字串 str

列表 list       #[1,2,3]

元組 tuple    #(1,2,3)

字典 dict      #{1:2}

集合 set       #set{[‘a‘,‘c‘,‘b‘]} 集合對象是一組無序排列的可雜湊的值:集合成員可以做字典的鍵

字串操作方法

1、大小寫變換

>>>a = "HEllo">>>a.lower() #小寫‘hello‘>>>a.upper() #大寫‘HELLO‘>>>a.swapcase() #交換大小寫‘heLLO‘>>>a.title() #標題化字串,所有單詞以大寫字母開始,其他小寫‘Hello‘>>>a.capitalize() #首字母大寫,其餘小寫‘Hello‘

2、字串運算

+ 字串串連

>>> a = ‘hello‘>>> b = ‘world‘>>> print(a+b)‘helloworld‘

* 重複輸出

>>> a = ‘abc‘>>> print(a*3)‘abcabcabc‘

[] 索引擷取字元

>>> a = ‘hello‘>>> print(a[2])l>>> print(a[0:2])he

in 成員運算子,如果字串中包含給定字元返回True

not in 成員運算子,如果字串中不包含給定字元返回True

 r/R 原始字串,沒有轉義特殊或不能列印

>>> print(r‘\nabc\n‘)\nabc\n>>> print(R‘\nabc\n‘)\nabc\n

% 格式字串

3、格式字串

%c 格式化字元及其ASCII碼

%s 格式化字串

%d 格式化整數

%u 格式化無符號整型

%o 格式化無符號八位元

%x 格式化無符號十六進位數

%X 格式化無符號十六進位數(大寫)

%f 格式化浮點數字,可指定小數點後的精度

%e 用科學計數法格式化浮點數

%E 作用同%e,用科學計數法格式化浮點

%g 根據值的大小決定使用%f或%e

%G 作用同%g,根據值的大小決定使用%f或%e

%p 用十六進位數格式化變數的地址

格式化操作符輔助:

* 定義寬度或小數精度

- 用作靠左對齊

+ 正數前面顯示+號

<sp>正數前面顯示空格

#八進位前顯示(‘0’),十六進位前顯示‘0x‘或者‘0X‘,取決於用x或X

0 顯示數字前面填充‘0‘而不是預設空格

% ‘%%‘輸出一個單一的’%‘

(var) 映射變數(字典參數)

m.n m顯示最小寬度,n小數點後的位元

4、三引號

‘‘‘ ‘‘‘三引號允許一個字串跨多行,字串中可以包含分行符號、定位字元以及其他特殊字元

5、Unicode字串

>>> u‘Hello\u0020World !‘u‘Hello World !‘

插入編碼值為 0x0020 的 Unicode 字元(空格符)

6、字串的內建函數

>>> s = ‘hello world‘>>> s.find(‘el‘)1
>>> s.find(‘el‘,0,3)
1

返回出現子字串的第一個字母標號,如果沒有返回-1;

s.find(substr,beg,end) 返回包含在beg end指定範圍內的索引值

 

>>> ‘This is a test‘.replace(‘is‘, ‘eez‘)  ‘Theez eez a test‘  

返回字串所有匹配項被替換後得到的字串

 

>>> ‘1+2+3+4+5‘.split(‘+‘)  [‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘]  >>> ‘/usr/bin/env‘.split(‘/‘)  [‘‘, ‘usr‘, ‘bin‘, ‘env‘]  >>> ‘Using the default‘.split()  [‘Using‘, ‘the‘, ‘default‘] 

將字串分割成序列

 

>>> seq = [‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘]  >>> sep = ‘+‘  >>> sep.join(seq)  ‘1+2+3+4+5‘  >>> dirs = ‘‘, ‘usr‘, ‘bin‘, ‘env‘  >>> ‘/‘.join(dirs)  ‘/usr/bin/env‘  

join在列表中添加元素

 

>>> ‘         internal whitespace is kept        ‘.strip()  ‘internal whitespace is kept‘  

strip去除兩側(不含內部)空格的字串

 

s.join

s.center(width)

s.count(str,beg,end)

s.index(str,beg,end) #和find方法一樣,str不在報異常

s.isalnum() #至少一個字元且所有字元為字母或數字返回True

s.isdecimal()

s.islower()

s.isnumeric()

s.isspace()

s.istitle()

s.isupper()

s.digits:包換數字 0 - 9 的字串

s.letters:包含所有字母(大寫和小寫)的字串

s.lowercase:包含所有小寫字母的字串

s.printable:包含所有可列印字元的字串

s.punctuation:包含所有標點的字串

s.uppercase:包含所有大寫字母的字串

 

ps:字串的方法非常多,需要在執行個體中熟練掌握

 

【python學習筆記01】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.