python 資料型別(元組 不可變列表 ,字串

2022-08-09 03:06:09 字數 2199 閱讀 8571

元組(不可變列表)

建立元組:

ages = (11, 22, 33, 44, 55)

或ages = tuple((11, 22, 33, 44, 55))

一般情況下是不需要被人該的數值才使用元組

r=(1,2,3,4,5,6,) #

唯讀列表,不可不可變

print(type(r)) #

檢視資料型別

字串

"

hello world

"

萬惡的字串拼接:

python中的字串在c語言中體現為是乙個字元陣列,每次建立字串時候需要在記憶體中開闢一塊連續的空,並且一旦需要修改字串的話,就需要再次開闢空間,萬惡的+號每出現一次就會在內從中重新開闢一塊空間。

字串格式化輸出

ps: 字串是 %s;整數 %d;浮點數%f

字串常用功能:

#

字串的使用

name="

wang,dlong,de,jia

"print(name[2:6]) #

切片取值

print(name.center(50,"

-")) #

格式列印,效果見列印資訊

print(name.find("

i")) #

如果找到會返回下標位置,找不到會提示為-1

print

(name.strip())

name2=name.split("

,") #

進行切割,按照逗號切割,也可以按照空格切割,切割後成了列表

name3=("

||".join(name2)) #

按照指定符號合併(這裡使用的||)

print

(name)

print

(name2)

print

(name3)

name4="

waggdelong

"print(name4.endswith("

g")) #

依照某個字母結束,判斷

print(name4.startswith("

w")) #

依照某個字母開始,判斷

print(name4.upper()) #

全部大寫

print(name4.lower()) #

全部小寫

print(name4.isalnum()) #

判斷字串的內容,如果不包含特殊字元,那麼就為真(true),包含就為假

print(''

in name4) #

判斷有沒有空格

print(name4.capitalize()) #

進行首字母大寫

msg = "

你好,,你的年齡

"msg2 = msg.format(name="

laowang

",age=28) #

format直接賦予變數值

print

(msg2)

msg3="

姓名:,年齡:"#

可以進行編號賦值看列印效果,次方法填寫簡單,不建議用到過長的字元多變數的字串裡面

print(msg3.format("

dawang

",45))

age = input("

your age:")

if age.isdigit(): #

判斷是否為數字

age =int(age)

else

:

print("

輸入錯誤")

#上面的方法無法驗證使用下面的方法

u_name=input("

suer:")

if u_name.strip() == "

wang

": #

strip 在這裡就是移除空白的意思,當不使用strip的時候在提示輸入的時候可以在後面多加乙個空格是不會列印資訊的

print("

你好嗎?

")

列印的內容是:

python 可變資料型別 不可變資料型別

在python中,資料型別分為可變資料型別和不可變資料型別,不可變資料型別包括string,int,float,tuple,可變資料型別包括list,dict。所謂的可變與不可變,舉例如下 a test print a 0 t a 0 1 traceback most recent call las...

python 可變資料型別 不可變資料型別

在python中,資料型別分為可變資料型別和不可變資料型別,不可變資料型別包括string,int,float,tuple,可變資料型別包括list,dict。所謂的可變與不可變,舉例如下 a test print a 0 t a 0 1 traceback most recent call las...

python資料型別 可變資料型別和不可變資料型別

在python中資料型別包含以下幾類 1.數字型別 number 整型 int 浮點型 float 複數 complex 2.字串型別 string 3.列表 list 4.集合 set 5.元組 tuple 6.字典 dict 上面的型別中屬於可變型別的是 dict,list,set 屬於不可變型...