1. 程式人生 > >python學習之基本數據類型

python學習之基本數據類型

pan 運行 pytho 所有 find AC 範圍 檢測 分隔

python的基本數據類型有數字、字符串、列表、字典、元祖、布爾值

一、數字

1.1、字符轉換為數字

實例:

a="123"
b=int(a)
print(b+100)

運行結果:

223

可以用type查看數據類型:

a="123"
print(type(a))
b=int(a)
print(type(b))
print(b+100)

運行結果:

<class str>
<class int>
223

二、字符串

2.1、join()

將字符串按照指定的字符進行拼接

實例:

#join(),將字符串按照指定的字符進行拼接
test="你女兒的媽媽的媽媽是誰" str_n="#" res=str_n.join(test) res1="&".join(test) print(res) print(res1)

運行結果:

#女#兒#的#媽#媽#的#媽#媽#是#誰
你&女&兒&的&媽&媽&的&媽&媽&是&誰

2.2、split()

split()通過指定分隔符對字符串進行切片,如果參數num 有指定值,則僅分隔 num 個子字符串

語法:

str.split(str="", num=string.count(str))

參數:

str -- 分隔符,默認為所有的空字符,包括空格、換行(\n)、制表符(\t)等。

num -- 分割次數。

返回值:字符串列表

實例:

test="as d ef d qwe d ytrdvcd"
v1=test.split(d,1)
v2=test.split(d,2)
v3=test.split(d,3)
v4=test.split(d,4)
print(v1)
print(v2)
print(v3)
print(v4)

結果:

[as, efdqwedytrdvcd]
[as, ef, qwedytrdvcd
] [as, ef, qwe, ytrdvcd] [as, ef, qwe, ytr, vcd]

2.3、find()方法

find() 方法檢測字符串中是否包含子字符串 str ,如果指定 beg(開始) 和 end(結束) 範圍,則檢查是否包含在指定範圍內,如果指定範圍內如果包含指定索引值,返回的是索引值在字符串中的起始位置。如果不包含索引值,返回-1。

語法:

str.find(str, beg=0, end=len(string))

參數:

str:指定檢索的字符串

beg:檢索的開始位置,默認為0

end:檢索的結束位置

返回值:

如果包含子字符串返回開始的索引值,否則返回-1。

實例:

test = "Weareallgoodfriends"
test1 = "good"
str=test.find(test1,0,len(test))
str1 = test.find(test1)
str2 = test.find(test1,2,19)
str3 = test.find(test1,2,5)
print(str)
print(str1)
print(str2)
print(str3)

運行結果:

8
8
8
-1

2.4、strip()方法

strip() 方法用於移除字符串頭尾指定的字符(默認為空格)。strip意思為清除、拆除、刪除的意思。

語法:

str.strip([chars])

參數:

chars -- 移除字符串頭尾指定的字符。

返回值:

返回移除字符串頭尾指定的字符生成的新字符串。

實例:

test="******Weareall***goodfriends*****"
test1="*"
str=test.strip(test1)
print(str)
print(test.strip(*))

運行結果:

Weareall***goodfriends
Weareall***goodfriends

2.5、lstrip()方法

strip() 方法用於移除字符串左邊指定的字符(默認為空格)。 left 為左邊的意思,strip意思為清除、拆除、刪除的意思。

語法:

str.strip([chars])

參數:

chars -- 移除字符串左邊指定的字符。

返回值:

返回移除字符串左邊指定的字符生成的新字符串。

實例:

test="******Weareall***goodfriends*****"
test1="*"
str=test.lstrip(test1)
print(str)
print(test.lstrip(*))

運行結果

Weareall***goodfriends*****
Weareall***goodfriends*****

2.6、rsrip()方法

strip() 方法用於移除字符串右邊指定的字符(默認為空格)。 right為右邊的意思,strip意思為清除、拆除、刪除的意思。

語法:

str.strip([chars])

參數:

chars -- 移除字符串右邊指定的字符。

返回值:

返回移除字符串右邊指定的字符生成的新字符串。

實例:

test="******Weareall***goodfriends*****"
test1="*"
str=test.rstrip(test1)
print(str)
print(test.rstrip("*"))

運行結果

******Weareall***goodfriends
******Weareall***goodfriends

2.7、upper()方法

upper() 方法將字符串中的小寫字母轉為大寫字母。

語法:

str.upper()

參數:沒有

返回值:

返回小寫字母轉為大寫字母的字符串。

實例:

test="Weareallgoodfriends"
print(test.upper())

運行結果:

WEAREALLGOODFRIENDS

2.8、lower()方法

lower() 方法將字符串中的大寫字母轉為小寫字母。

語法:

str.lower()

參數:沒有

返回值:

返回大寫字母轉為小寫字母的字符串。

實例:

test="WeareallGOODfriends"
print(test.upper())

運行結果:

WEAREALLGOODFRIENDS

2.9、replace()方法

replace() 方法把字符串中的 old(舊字符串) 替換成 new(新字符串),如果指定第三個參數max,則替換不超過 max 次。

語法:

str.replace(old, new[, max])

參數:

old -- 將被替換的子字符串。

new -- 新字符串,用於替換old子字符串。

max -- 可選字符串, 替換不超過 max 次

返回值:

返回字符串中的 old(舊字符串) 替換成 new(新字符串)後生成的新字符串,如果指定第三個參數max,則替換不超過 max 次。

實例:

test = "wertrecdezseytrer"
v = test.replace("e",#)
print(v)
v = test.replace("e",#,2)
print(v)

運行結果:

w#rtr#cd#zs#ytr#r
w#rtr#cdezseytrer

python學習之基本數據類型