數字型別+串列型別
a=10 其實等于a=int() int() 前面是名字,后面是括號,相當于呼叫前面的工廠生產一個括號里的東西 通過int造一個10 賦值給a
相當于呼叫int傳一個引數10 賦值給a
a=input( ) 通過input 把用戶輸入的東西造出來 賦值給a
a = 10
print(a)
b = input()
print(b)
c = print("hello world") #print不生產東西
print(c) #是空的 print不會生產一個字串賦值給變數c
10
23 #輸入23
23
hello world
None
資料型別轉換
整型 int
定義型別 型別的轉換
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 system
八進制 Octal number system
十進制 Decimal system
十六進制 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/142968.html
標籤:其他
上一篇:go語言摘記
下一篇:5-python變數型別
