Python 內建資料型別(二)

2021-09-02 22:57:33 字數 2922 閱讀 8681

一、字串(不可改變物件)

1、定義:有序的字元的集合,用單引號或者雙引號表示,支援索引,切片

就是編號,通過這個編號就能找到相應的儲存空間。

正數從左到右,負數從右到左。

切片:sx[:],sx[:3],sx[2:],sx[0:4:2]

是指對操作物件擷取其中的一部分

語法:[起始:結束:步長:]

注意:選取的區間屬於左閉右開,即從『起始』位開始,到『結束』位的前一位結束(不包含結束位本身)。

元素存在性:『a』 in sx,『b』 not in sy

函式操作:設mystr = 『hello world itcast and itcastcpp』

mystr.

find

(str,start=

0,end=

len(mystr)

)

mystr.

index

(str,start=

0,end=

len(mystr)

)

mystr.

count

(str,start=

0,end=

len(mystr)

)

mystr.

replace

(str1,str2,mystr.

count

(st1)

)

mystr.

split

(str="",

2)

mystr.

capitalize

()

mystr.

title

()

mystr.

startswith

(obj)

mystr.

endswith

(obj)

mystr.

lower

()

mystr.

upper

()

mystr.

ljust

(wdith)

mystr.

rjust

(wdith)

mystr.

center

(width)

mystr.

lstrip

()

mystr.

rstrip

()

mystr.

strip

()

mystr.

rfind

(str,start=

0,end=

len(mystr)

)

mystr.

rindex

(str,start=

0,end=

len(mystr)

)

mystr.

partition

(str)

mystr.

rpartition

(str)

mystr.

splitlines

()

mystr.

isalpha

()

mystr.

isdigit

()

mystr.

isalnum

()

mystr.

isspace

()

mystr.

join

(str)

raw字串:全部化為普通字元r』\n\t\a 123abc』

二、列表-list(可變物件)函式操作:

sx.

(obj)

sx.

extend

(obj)

sx.

insert

(index,object)

del sx[0]

del sx

sx.

pop(

)

sx.

remove

(obj)

sx.

sort()

sx.sort

(reverse=

true

)sx.

sort

(key=str.lower,reserve=

true

)

sx.

reverse

()

三、元組-tuple(不可變物件)

定義:其餘與list相同,用()表示

四、字典(可變物件)附錄:

內建函式:

Python 內建資料型別

1 字串 string 1 str this is string print str print type str 2 str 想怎麼些就怎麼些 怎麼的 print str print type str 注意 單引號 雙引號均可,使用三個引號,其內容可以自由書寫 2 布林型別 bool bool f...

python內建資料型別 數字

型別 子型別示例 相關模組 數字整數 2 100 2的100次方 import math import random 浮點數複數等 小數物件 表現固定精度的特性 例如 頭寸的合計 以及實現更好的數字精度是乙個理想的工具 0.1 0.1 0.1 0.3 5.551115123125783e 17 fr...

Python內建資料型別總結

在眾多語言的學習和應用中,資料型別必不可少,當然了核心資料型別也無外乎就那麼幾種.很多語言中提供了數字,字串,檔案資料型別,有一部分形式的資料型別以標準庫的形式表示 但是在 python 有很多資料型別都是內建的,不需要 import 數字字串 列表list 字典dict 元組集合 none 布林檔...