7-(數字類型+字符串類型)內置函數

數字類型+列表類型python

a=10 其實等於a=int() int() 前面是名字,後面是括號,至關於調用前面的工廠生產一個括號裏的東西 經過int造一個10 賦值給agit

至關於調用int傳一個參數10 賦值給aapi

a=input( ) 經過input 把用戶輸入的東西造出來 賦值給aide

a = 10
print(a)
b = input()
print(b)
c = print("hello world") #print不生產東西
print(c)  #是空的  print不會生產一個字符串賦值給變量c

10
23 #輸入23
23
hello world
Nonespa

數據類型轉換

整型 int

定義類型 類型的轉換code

test = int('123456')
print(test, type(test))
print(bin(120)) # 十進制轉換爲二進制  零B (bin)開頭的爲二進制
print(oct(120)) # 十進制轉換爲八進制  零o (oct)表示八進制
print(hex(120)) # 十進制轉換爲十六進制 零x(hex)表示十六進制
print(int('0b1111000', 2)) #二進制轉換爲十進制
print(int('0o170', 8)) #八進制轉換爲十進制
print(int('0x78', 16)) #十六進制轉換爲十進制

123456 <class 'int'>
0b1111000
0o170
0x78
120
120
120索引

二進制 Binary systemip

八進制 Octal number systemci

十進制 Decimal systemunicode

十六進制 Hexadecimal

浮點型
test1 = 3.12345 #  等於 test1=folat(3.12345)  定義類型
test = float('3.1415926')  # 類型轉換
print(test, type(test), type(test1))

字符串類型

定義
a = 'hell world' # 等於 = str('hello world')
print(type(a))

a1 = str('hello world')
print(type(a1))

<class 'str'>
<class 'str'>

類型轉換

字符串類型 str 能夠把任意類型轉換爲字符串類型

a = str([1, 2, 3, 4])
print(type(a))
a1 = str(12345)
print(type(a1))
a2 = str(3.1415926)
print(type(a2))
a3 = str({'a1': 1, 'a2': 3.1415926, 'a3': 'hahah'})
print(type(a3))

[1, 2, 3, 4] <class 'str'>
12345 <class 'str'>
3.1415926 <class 'str'>
{'a1': 1, 'a2': 3.1415926, 'a3': 'hahah'} <class 'str'>

索引取值
a = "Hello Man,Girl,Woman"
print(a[0], a[5])  # 正向取值 空格也是字符
print(a[-1], a[-6]) # 反向取值
a[0] = 'h'

H
n ,

切片取值
a = "Hello Man,Girl,Woman"
print(a[0:6])  # 只能取值到0 1 2 3 4 5   顧頭不顧尾
print(a) # 不改變原來變量的值

Hello
Hello Man,Girl,Woman

步長及反步長
a = "Hello Man,Girl,Woman"
print(a[0:6])  # 默認步長是1 # 取到第五個值 空格
print(a[0:6:2])  # 步長是2 取值是H l o 
print(a[6:0:-2])  # 反向步長,當使用反向步長的時候 至關於倒着走 前面也須要反向 取不到0值全部沒有H   
print(a[:])  # 默認是從頭取到尾  步長默認是1
print(a[::-1])  # 至關於字符串反轉

Hello #這裏還有一個空格的值
Hlo
Mol #取到的是第六個值 M 第四個 o 第三個l 顧頭不顧尾 H 取不到

Hello Man,Girl,Woman
namoW,lriG,naM olleH

長度計算 len
a = "Hello Man,Girl,Woman" #共20個字符
print(len(a))

20

成員運算 in not in

判斷某個字符是否在一個字符串中

a = "Hello Man,Girl,Woman"
print('H' in a)
print('o' not in a)

True
False

移除空格 strip
a = '         woman       '
a1 = '&&&&&&&&&&&&woman&&&&&&&&&&&'
a2 = '&&&&&&&&&&&&wo&&&man&&&&&&&&'
a3 = '7#^%^woman*&^%&%'
print(a.strip()) # 能夠去除左右兩側的空格
print(a)  # 不會改變原來變量的值
print(a1.strip('&')) #能夠去除指定的符號
print(a2.strip('&')) #中間的符號沒法去除
print(a3.strip('7#^%^*&')) # 能夠去除多種符號

woman
woman
woman
wo&&&man
woman

去除空格的部分能夠應用於用戶輸入

a = input('請輸入用戶密碼: ').strip() 防止用戶輸入出現空格致使用戶名密碼錯誤

字符串切分 split rsplit 字符串切成列表

把字符串按照某種分隔符隔開造成新的列表

a = "Hello Man,Girl,Woman"
print(a.split())  # 默認按照空格分割
print(a.split(','))  # 按照指定符號分割
print(a.split(',', 1))  # 指定分割次數爲1次

['Hello', 'Man,Girl,Woman']
['Hello Man', 'Girl', 'Woman']
['Hello Man', 'Girl,Woman']

a = "Hello Man,Girl,Woman"
print(a.rsplit(',', 1))  #從右到左邊尋找,按,分割,分割一次
print(a.split(',', 1))  #從左向右尋找 默認的 按,分割  分割一次

['Hello Man,Girl', 'Woman']
['Hello Man', 'Girl,Woman']

列表還原爲字符串 join

列表拼接

a = ['Hello Man', 'Girl,Woman']
a1 = " ".join(a) #以空格形式鏈接列表a爲字符串
a2 = ':'.join(a) #以:形式鏈接列表a爲字符串
print(a1, a2)
print(type(a1), type(a2))

Hello Man Girl,Woman Hello Man:Girl,Woman
<class 'str'> <class 'str'>

循環遍歷

a = "Hello,"
for i in a:
    print(i)

H
e
l
l
o
,

strip lstrip rstrip
a = '&&&&&&&&&&&&wo&&&man&&&&&&&&'
print(a.strip('&'))# 清除左右兩側
print(a.lstrip('&')) #清除左側
print(a.rstrip('&')) # 清除右側

wo&&&man
wo&&&man&&&&&&&&
&&&&&&&&&&&&wo&&&man

lower upper 大小寫轉換
a = "Hello Man,Girl,Woman"
print(a.lower()) #都變成小寫
print(a.upper()) #都變成大寫

hello man,girl,woman
HELLO MAN,GIRL,WOMAN

startswitch endswith 判斷開頭結尾
a = "Hello Man,Girl,Woman"
print(a.startswith('H'))
print(a.endswith('man'))
print(a.endswith('n'))

True
True
True

replace字符串替換
a = "Hello Man,Girl,Woman"
print(a.replace('l', 'L'))#.replace字符串替換  前面是舊的 後面是新的 默認所有匹配替換
print(a.replace('n', 'N', 1))  #後面的數字表示替換幾回 n-N替換一次
print(a.replace('n', 'N', 2)) #n-N替換2次  默認是所有替換的

HeLLo Man,GirL,Woman
Hello MaN,Girl,Woman
Hello MaN,Girl,WomaN

isdigit
判斷是否純數字
print('12345'.isdigit())#判斷是不是純數字,用於用戶年齡,工資等純數字的判斷
print('12qwe'.isdigit())

True
False

age = input("請輸入年齡")
if age.isdigit():
    int(age) > 0
print("real")

請輸入年齡23
real

find rfind index rindex count
a = "Hello Man,Girl,Woman "
print(a.find('n')) #第一個n出現的索引位置
print(a.rfind('n')) #最後一個n出現的索引位置
print(a.index('n')) #第一個n出現的索引位置
print(a.rindex('n'))  #最後一個n出現的索引位置
print(a.find('A')) # 找不到的時候返回-1
print(a.count('n')) #統計n出現的次數
#print(a.index('A')) # 找不到會報系統異常錯誤error

8
19
8
19
-1
2

center ljust rjust zfill 對齊輸出
print('alin'.center(10, '&'))  # 字符串居中對齊 填充*
print('alin'.ljust(10, '&'))  # 字符串左對齊 填充*
print('alin'.rjust(10, '&'))  # 字符串右對齊 填充*
print('alin'.zfill(10))  # 原字符串右對齊,前面填充0

&&&alin&&&
alin&&&&&&
&&&&&&alin
000000alin

expandtabs 製表符
a = "Hello\tMan\tGirl\tWoman"
print(a.expandtabs(8))  # 把字符串中\t轉化爲空格  8個空格

Hello Man Girl Woman

captalize swapcase title大小寫轉換
print('hello woman'.capitalize()) #只留下 第一個首字母大寫 
print('HELLO WOMAN'.capitalize()) #只留下 第一個首字母大寫
print('hello woman'.swapcase()) #全部字母通通大寫  大小寫所有轉換
print('HELLO WOMAN'.swapcase()) #全部字母通通小寫 大小寫所有轉換
print('hello woman'.title()) #全部單詞首字母大寫
print('HELLO WOMAN'.title())  #全部單詞首字母大寫

Hello woman
Hello woman
HELLO WOMAN
hello woman
Hello Woman
Hello Woman

is 數字識別 isnumberic isdigit isdecimal
is系列 判斷真假
print('alinx'.islower()) #islower 都是小寫才爲真
print('ALinx'.isupper()) #isupper 都爲大寫才爲真
print('Hello world'.istitle()) #.istitle  全部單詞首字母大寫才爲真
print('2121swsawe'.isalnum()) # 字符串由字母或數字組成結果爲True 不能有其它的
print('awqqd'.isalpha()) # 字符串由由字母組成結果爲True 必須只有字母
print('a34d'.isalpha()) # false
print('     '.isspace())  # 字符串只有空格組成結果爲True
print('plased'.isidentifier())# (a-z)和(0-9)或下劃線(_)組成  不能以數字開頭,能夠用來檢測變量名是否合規
print('fdfdsafads2132'.isidentifier())
print('12323wdsa'.isidentifier())

True
False
False
True
True
False
True
True
True
False

數字判斷

num1=b'6' #bytes
num2=u'6' #unicode,python3中無需加u就是unicode
num3='六' #中文數字
num4='Ⅳ' #羅馬數字

isdigit

只能識別二進制和unicode 不認識漢字數字和羅馬數字

num1=b'4' 
num2=u'4' 
num3='四' 
num4='Ⅳ' 
print(num1.isdigit()) 
print(num2.isdigit()) 
print(num3.isdigit()) 
print(num4.isdigit())

True
True
False
False

isnumeric

能夠識別 漢字數字 羅馬數字 unicode數字 最強

num1=b'4' 
num2=u'4' 
num3='四' 
num4='Ⅳ' 
#print(num1.isnumeric())  #byte數字直接報錯
print(num2.isnumeric()) # True
print(num3.isnumeric()) # True
print(num4.isnumeric()) # True

True
True
True

isdecimal 只能識別 unicode數字
num1=b'4'
num2=u'4' 
num3='四' 
num4='Ⅳ' 
#print(num1.isdecimal()) # byte直接報錯
print(num2.isdecimal()) # True
print(num3.isdecimal()) # True
print(num4.isdecimal()) # True

True False False

相關文章
相關標籤/搜索