python學習之基本數據類型

 

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

1、數字

1.一、字符轉換爲數字

實例:spa

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

運行結果:code

223

能夠用type查看數據類型:blog

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

運行結果:索引

<class 'str'>
<class 'int'>
223

 2、字符串

2.一、join()

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

實例:字符串

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

 運行結果:string

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

 2.二、split()

split()經過指定分隔符對字符串進行切片,若是參數num 有指定值,則僅分隔 num 個子字符串it

語法:class

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.三、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.四、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.五、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.六、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.七、upper()方法

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

語法:

str.upper()

參數:沒有

返回值:

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

實例:

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

 

運行結果:

WEAREALLGOODFRIENDS

 

2.八、lower()方法

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

語法:

str.lower()

參數:沒有

返回值:

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

實例:

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

 

運行結果:

WEAREALLGOODFRIENDS

 

2.九、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

 

 2.十、isalnum()

判斷是不是字母加數字的組合

name="He12345_123"
v = name.isalnum()
print(v)

False
相關文章
相關標籤/搜索