新聞中心
python數(shù)據(jù)類型:
- Number(數(shù)字)
- String(字符串)
- List(列表)
- Dictonary(字典)
- Tuple(元組)
- sets(集合)
其中數(shù)字、字符串、元組是不可變的,列表、字典是可變的。
對(duì)不可變類型的變量重新賦值,實(shí)際上是重新創(chuàng)建一個(gè)不可變類型的對(duì)象,并將原來的變量重新指向新創(chuàng)建的對(duì)象(如果沒有其他變量引用原有對(duì)象的話(即引用計(jì)數(shù)為0),原有對(duì)象就會(huì)被回收)。
創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比蚌埠網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式蚌埠網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋蚌埠地區(qū)。費(fèi)用合理售后完善,十載實(shí)體公司更值得信賴。
數(shù)字
- int:整數(shù)
?? 1.正負(fù)數(shù)
? ?2.十六進(jìn)制(表示方式為0x或者0X開頭。例如:0xff)
? ?3.八進(jìn)制(表示方式為0o或者0O開頭。例如:0o632457)
?? 4.二進(jìn)制 (表示方式為0b或者0B開頭。例如:0b101100) - fraction:分?jǐn)?shù)
- float:浮點(diǎn)數(shù)
- complex:復(fù)數(shù)
- bool:布爾型(特殊的數(shù)值類型,只有True和False兩個(gè)值)
進(jìn)制轉(zhuǎn)換
整數(shù)轉(zhuǎn)其他進(jìn)制 使用bin(i),oct(i),hex(i)函數(shù)可以將十進(jìn)制數(shù)分別轉(zhuǎn)換為二進(jìn)制,八進(jìn)制,十六進(jìn)制
>>> s=10 >>> bin(s) '0b1010' >>> oct(s) '0o12' >>> hex(s) '0xa'
使用int(str,base)可以將非十進(jìn)制的數(shù)轉(zhuǎn)換成整數(shù)
,其中str是文本形式的數(shù)字,base可以為2,8,16數(shù)字,分別代表二進(jìn)制,八進(jìn)制,十六進(jìn)制,最高到36位,最低為2>>> int('0b10010',2) 18 >>> int('0o52415',8) 21773 >>> int('0x134ab',16) 79019 >>> int('s',32) 28 >>> int('yz',36) 1259
當(dāng)然也可以進(jìn)行16進(jìn)制轉(zhuǎn)二進(jìn)制八進(jìn)制,八進(jìn)制可以轉(zhuǎn)其他進(jìn)制
>>> hex(0b1001) '0x9' >>> hex(0o1234) '0x29c' >>> oct(0b101) '0o5' >>> oct(0xff) '0o377' >>> bin(0xff) '0b11111111' >>> bin(0o7777) '0b111111111111'
各類運(yùn)算符
- 算數(shù)運(yùn)算符:+,-,*,/,%.//,**
- 比較運(yùn)算符:==,!=,>,<,<=,>=
- 賦值運(yùn)算符:=,+=,-=,*=,/=,%=,//=.**=
- 位運(yùn)算符:&,|,^,~,<<,>>
- 邏輯運(yùn)算符:and,or,not
- 成員運(yùn)算符:in,not in
- 身份運(yùn)算符:is,is not
>>> a=12 >>> f=~a >>> f -13 >>> bin(f) '-0b1101' >>> bin(a) '0b1100' >>> bin(a<<1) '0b11000' >>> bin(a>>1) '0b110' >>> list=[1,2,3,4,5] >>> a=3 >>> print (a in list) True >>> print (a not in list) False >>> a=['1,2,3,4,5'] >>> b=a >>> print (b is a ) True >>> print (b is not a ) False >>> b=a[:] >>> print (b is a) #這是因?yàn)樽址遣豢勺兊?False >>> print (id(a)) 42473480 >>> print (id(b)) 42485000
運(yùn)算符優(yōu)先級(jí)
- ** (優(yōu)先級(jí)最高的是冪運(yùn)算)
- ~,+,- (加和減都是一元運(yùn)算符)
- *,/,%,//
- +,-
- <<,>>
- &
- ^,|
- <=,>=,<,>
- ==,!=
- =,+=,-=,*=,/=,%=,//=,**=
數(shù)學(xué)函數(shù)的應(yīng)用
- power:冪函數(shù),功能與運(yùn)算符**一樣
>>> pow(2,3) 8
- sqrt:取當(dāng)前數(shù)的平方根
>>> import math >>> math.sqrt(4) 2.0
- max:最大值
>>>max(2,3,4,5,1,9,6) 9
- min:最小值
>>> min(2,3,4,5,1,9,6) 1
- abs與fabs:取絕對(duì)值,fabs取出的是浮點(diǎn)數(shù)
>>> abs(-1) 1 >>> math.fabs(-1) 1.0
- round:四舍五入(當(dāng)小數(shù)為5的時(shí)候會(huì)向靠近偶數(shù)的一端進(jìn))
>>> round(3.5) 4 >>> round(2.5) 2 >>> round(2.54) 3 >>> round(2.45) 2
- ceil:向上取整
>>> math.ceil(1.7) 2 >>> math.ceil(1.3) 2
- floor:向下取整
>>> math.floor(1.7) 1 >>> math.floor(1.3) 1
- cmp:python2中的比較函數(shù),當(dāng)前面數(shù)值大返回-1,一樣大返回0,后面數(shù)值大返回1
>>> cmp(1,2) -1 >>> cmp(1,1) 0 >>> cmp(2,1) 1
- 隨機(jī)數(shù)函數(shù)
??- 取0-1之間的隨機(jī)小數(shù):>>> import random >>> random.random() 0.18001643527271916
??????- 取自定義數(shù)里的隨機(jī)數(shù):(可以取多個(gè)元素)
>>> random.choice([1,2,3,4,5]) 2 >>> random.choice([1,2,3,4,5]) 3 >>> random.sample([1,2,3,4,5,6,7,8,9],2) [3, 7] >>> random.sample([1,2,3,4,5,6,7,8,9],3) [4, 9, 3]
??????- 隨機(jī)打亂順序:
>>> a=[1,2,3,4,5,8] >>> random.shuffle(a) >>> a [1, 8, 2, 3, 4, 5]
??????- 獲取N位隨機(jī)數(shù):(二進(jìn)制)
>>> random.getrandbits(6) 55 >>> random.getrandbits(6) 48 >>> random.getrandbits(7) 104
- modf:把浮點(diǎn)數(shù)的整數(shù)位和小數(shù)位單獨(dú)取出來
>>> math.modf(1.4) (0.3999999999999999, 1.0) >>> math.modf(1.5) (0.5, 1.0) >>> math.modf(2.8) (0.7999999999999998, 2.0) >>> math.modf(3.1) (0.10000000000000009, 3.0)
- log:指數(shù)函數(shù)。默認(rèn)e為底數(shù),結(jié)果為浮點(diǎn)數(shù)。也可以自定義底數(shù)
>>> math.log(4,2) 2.0 >>> math.log2(4) 2.0 >>> math.log10(100) 2.0 >>> math.log(100,10) 2.0
- 格式化輸出:格式化輸出保留有效數(shù)字,格式化輸出的是字符串
>>> s=format(2.345,'0.2f') >>> s >>> type (s)
>>> round(2.5) 2 >>> format(2.5,'0.0f') '2' - Decimal模塊:在使用浮點(diǎn)數(shù)的時(shí)候,因?yàn)橛?jì)算機(jī)是使用二進(jìn)制表示,所以會(huì)出現(xiàn)精度問題,可以使用Deciamal模塊來解決精度問題
>>> a=4.2 >>> b=2.1 >>> a+b 6.300000000000001 >>> from decimal import Decimal >>> a=Decimal('2.1') >>> b=Decimal('4.2') >>> a+b Decimal('6.3')
- 格式化輸出——format:使用format進(jìn)行進(jìn)制轉(zhuǎn)換
>>> a=20 >>> bin(a) '0b10100' >>> oct(a) '0o24' >>> hex(a) '0x14' >>> format(a,'b') '10100' >>> format(a,'o') '24' >>> format(a,'x') '14'
字符串
字符串(python2默認(rèn)使用ascii編碼,使用Unicode編碼須在字符串前加u,python3使用unicode編碼)
a='str'
a=u'str'字符串表示方法
- 單引號(hào):'str' '1'
- 雙引號(hào):"str""1"
- 三引號(hào):'''...str...''' """...str..."""
- 轉(zhuǎn)義字符:“str1 \tadded tab\nstr2”
Raw字符串:r"C:\user\administrator"(無法進(jìn)行轉(zhuǎn)義操作)
字符串操作
字符串合并
>>> 'abc'+'def' 'abcdef' >>> 'hello' *5 'hellohellohellohellohello' >>> print ('-'*50) -------------------------------------------------- >>> "aa""bb" 'aabb' >>> 'ab''cd' 'abcd'
字符串取值
a="text" >>> for c in a: ... print (c,end='') ... text >>> for c in a: ... print (c,end='-') ... t-e-x-t- >>> 'x' in a True >>> text='this_is_str' >>> text[0:4] 'this' >>> text[5:7] 'is' >>> text[:4] 'this' >>> text[-3:] 'str' >>> text[-12:-7] 'this' >>> text[::2] 'ti_***' >>> text[8:1:-2] 'ss_i'
字符串編碼轉(zhuǎn)換
>>> ord('d') 100 >>> chr(99) 'c' >>> ord('王') 29579 >>> chr(29579) '王'
字符串大小寫轉(zhuǎn)換
- 這里利用ascii編碼進(jìn)行大小寫轉(zhuǎn)換
>>> Text="" #初始化Text >>> text="aSdFgHjK" >>> for i in text: ... i_code=ord(i) ... if 97<=i_code and i_code<=122: ... Text+=chr(i_code-32) ... else: ... Text+=i ... >>> Text 'ASDFGHJK' >>> for x in text: ... x_code=ord(x) ... if 65<=x_code and x_code<=90: ... Text+=chr(x_code+32) ... else: ... Text+=x ... >>> Text 'ASDFGHJKasdfghjk'
- 這里利用字符串的方法進(jìn)行轉(zhuǎn)換
>>> str='asdFGHzxcVBN' >>> str.replace('asd','ASD') 'ASDFGHzxcVBN'
- 除此之外,還可以使用字符串的大小寫方法進(jìn)行大小寫轉(zhuǎn)換
ascii編碼對(duì)照表 二進(jìn)制 | 十進(jìn)制 | 十六進(jìn)制 | 圖形 |
---|---|---|---|
0010 0000 | 32 | 20 | (空格) |
0010 0001 | 33 | 21 | ! |
0010 0010 | 34 | 22 | " |
0010 0011 | 35 | 23 | # |
0010 0100 | 36 | 24 | $ |
0010 0101 | 37 | 25 | % |
0010 0110 | 38 | 26 | & |
0010 0111 | 39 | 27 | '' |
0010 1000 | 40 | 28 | ( |
0010 1001 | 41 | 29 | ) |
0010 1010 | 42 | 2A | * |
0010 1011 | 43 | 2B | + |
0010 1100 | 44 | 2C | , |
0010 1101 | 45 | 2D | - |
0010 1110 | 46 | 2E | . |
0010 1111 | 47 | 2F | / |
0011 0000 | 48 | 30 | 0 |
0011 0001 | 49 | 31 | 1 |
0011 0010 | 50 | 32 | 2 |
0011 0011 | 51 | 33 | 3 |
0011 0100 | 52 | 34 | 4 |
0011 0101 | 53 | 35 | 5 |
0011 0110 | 54 | 36 | 6 |
0011 0111 | 55 | 37 | 7 |
0011 1000 | 56 | 38 | 8 |
0011 1001 | 57 | 39 | 9 |
0011 1010 | 58 | 3A | : |
0011 1011 | 59 | 3B | ; |
0011 1100 | 60 | 3C | < |
0011 1101 | 61 | 3D | = |
0011 1110 | 62 | 3E | > |
0011 1111 | 63 | 3F | ? |
0100 0000 | 64 | 40 | @ |
0100 0001 | 65 | 41 | A |
0100 0010 | 66 | 42 | B |
0100 0011 | 67 | 43 | C |
0100 0100 | 68 | 44 | D |
0100 0101 | 69 | 45 | E |
0100 0110 | 70 | 46 | F |
0100 0111 | 71 | 47 | G |
0100 1000 | 72 | 48 | H |
0100 1001 | 73 | 49 | I |
0100 1010 | 74 | 4A | J |
0100 1011 | 75 | 4B | K |
0100 1100 | 76 | 4C | L |
0100 1101 | 77 | 4D | M |
0100 1110 | 78 | 4E | N |
0100 1111 | 79 | 4F | O |
0101 0000 | 80 | 50 | P |
0101 0001 | 81 | 51 | Q |
0101 0010 | 82 | 52 | R |
0101 0011 | 83 | 53 | S |
0101 0100 | 84 | 54 | T |
0101 0101 | 85 | 55 | U |
0101 0110 | 86 | 56 | V |
0101 0111 | 87 | 57 | W |
0101 1000 | 88 | 58 | X |
0101 1001 | 89 | 59 | Y |
0101 1010 | 90 | 5A | Z |
0101 1011 | 91 | 5B | [ |
0101 1100 | 92 | 5C | \ |
0101 1101 | 93 | 5D | ] |
0101 1110 | 94 | 5E | ^ |
0101 1111 | 95 | 5F | _ |
0110 0000 | 96 | 60 | ` |
0110 0001 | 97 | 61 | a |
0110 0010 | 98 | 62 | b |
0110 0011 | 99 | 63 | c |
0110 0100 | 100 | 64 | d |
0110 0101 | 101 | 65 | e |
0110 0110 | 102 | 66 | f |
0110 0111 | 103 | 67 | g |
0110 1000 | 104 | 68 | h |
0110 1001 | 105 | 69 | i |
0110 1010 | 106 | 6A | j |
0110 1011 | 107 | 6B | k |
0110 1100 | 108 | 6C | l |
0110 1101 | 109 | 6D | m |
0110 1110 | 110 | 6E | n |
0110 1111 | 111 | 6F | o |
0111 0000 | 112 | 70 | p |
0111 0001 | 113 | 71 | q |
0111 0010 | 114 | 72 | r |
0111 0011 | 115 | 73 | s |
0111 0100 | 116 | 74 | t |
0111 0101 | 117 | 75 | u |
0111 0110 | 118 | 76 | v |
0111 0111 | 119 | 77 | w |
0111 1000 | 120 | 78 | x |
0111 1001 | 121 | 79 | y |
0111 1010 | 122 | 7A | z |
0111 1011 | 123 | 7B | { |
0111 1100 | 124 | 7C | \ |
0111 1101 | 125 | 7D | } |
0111 1110 | 126 | 7E | ~ |
字符串方法
字符串大小寫相關(guān)的方法
- capitalize():字符串首字母大寫
>>> str='hello world' >>> str.capitalize() 'Hello world'
- title():字符串中單詞的首字母大寫
>>> str.title() 'Hello World'
- upper():字符串轉(zhuǎn)換成大寫
>>> str.upper() 'HELLO WORLD'
- lower():字符串轉(zhuǎn)換成小寫
>>> str.lower() 'hello world'
- swapcase():字符串大小寫互轉(zhuǎn)
>>> str='HellO wORld' >>> str.swapcase() 'hELLo WorLD'
字符串排版相關(guān)的方法
- center():居中對(duì)齊
>>> str='hello' >>> str.center(11) ' helloo ' >>> str.center(11,'_') '___helloo__'
- ljust():居左對(duì)齊
>>> str.ljust(11,'_') 'helloo_____' >>> str.ljust(11) 'helloo
- rjust():居右對(duì)齊
>>> str.rjust(11) ' hello' >>> str.rjust(11,'_') '_____hello'
- expandtabs():修改tab空格的個(gè)數(shù)
>>> str='hello\tworld' >>> print (str) hello world >>> str.expandtabs(9) 'hello world' >>> str.expandtabs(4) 'hello world'
- zfill():將字符串?dāng)U充到指定長(zhǎng)度,前面使用0填充
>>> str.zfill(20) '000000000hello\tworld' >>> 'sad'.zfill(10) '0000000sad'
- strip():刪除字符串兩邊(左邊lstrip或右邊rstrip)的指定字符(默認(rèn)為空格和換行符)
>>> str=' hello world ' >>> str.strip() 'hello world' >>> str.lstrip() 'hello world ' >>> str.rstrip() ' hello world' >>> str='hello,world' >>> str.strip('h') 'ello,world' >>> str.strip('[held]') 'o,wor'
字符串查找相關(guān)的方法
- startswith(prefix[,start[,end]])/endswith(suffix[,start[,end]]) 判斷是否以特定字符串開頭或者結(jié)尾
>>> str='hello,world' >>> str.startswith('hello') True >>> str.startswith('hello',0,5) True >>> str.startswith('hello',1,5) False >>> str.endswith('rld',8) True >>> str.endswith('rld',9) False >>> str.endswith('rld',8,11) True
- expandtabs():修改tab空格的個(gè)數(shù)
- count(sub[,start[,end]]):相應(yīng)字符串在文本中的個(gè)數(shù)
>>> str='hello,world' >>> str.count('l') 3 >>> str.count('ll') 1
- find/rfind():分別從字符串前后開始查找第一個(gè)匹配到的字符串的位置,找不到就返回-1
str='hello,world' >>> str.find('l') 2 >>> str.rfind('l') 9
- index/rindex():與find方法類似,但是找不到會(huì)報(bào)錯(cuò)
>>> str.index('l') 2 >>> str.rindex('l') 9 >>> str.index('a') Traceback (most recent call last): File "
", line 1, in ValueError: substring not found >>> str.find('a') - replace(old,new[,count]):替換字符串,count代表替換個(gè)數(shù)
>>> str.replace('l','L') 'heLLo,worLd' >>> str.replace('l','L',1) 'heLlo,world'
格式判斷相關(guān)方法
- isalpha() :判斷是否是字母
- isdigit():判斷是否是數(shù)字
- isalnum():判斷是否是數(shù)字和字母
- islower():判斷是否有字母,且字母為小寫字母
- isupper():判斷是否有字幕,且字母為大寫字母
- isspace():判斷是不是只有空格和換行符號(hào)
- istitle():判斷字符串每個(gè)單詞的首字母是否大寫
- isdecimal():判斷是不是數(shù)字
- isnumeric():判斷是不是數(shù)字
- isidentifier():判斷字符能否成為標(biāo)識(shí)符
- isprintable():判斷字符是否全部能打印的
isdigit、isdecimal、isnumeric三者的區(qū)別
isdigit()
True: Unicode數(shù)字,byte數(shù)字(單字節(jié)),全角數(shù)字(雙字節(jié)),羅馬數(shù)字
False: 漢字?jǐn)?shù)字
Error: 無isdecimal()
True: Unicode數(shù)字,,全角數(shù)字(雙字節(jié))
False: 羅馬數(shù)字,漢字?jǐn)?shù)字
Error: byte數(shù)字(單字節(jié))isnumeric()
True: Unicode數(shù)字,全角數(shù)字(雙字節(jié)),羅馬數(shù)字,漢字?jǐn)?shù)字
False: 無
Error: byte數(shù)字(單字節(jié))字符串分隔
- split([sep[,maxsplit]])/rsplit([sep[,maxsplit]]):分別從左右按照sep字符串分隔,最多分隔maxsplit此,默認(rèn)無數(shù)次
>> str='asd,fgh,jkl' >> str.split(',') ['asd', 'fgh', 'jkl'] >> str.rsplit(',',1) ['asd,fgh', 'jkl']
- splitlines()以\n或者\(yùn)r或者\(yùn)n\r分隔
>> str='asd\nfgh\njkl' >> str.splitlines() ['asd', 'fgh', 'jkl']
- partition(sep):將分隔符也作為一個(gè)元素列出來
>> 'http://www.baidu.com'.partition('://') ('http', '://', 'www.baidu.com')
字符串其他方法
- join():以特定的分隔符將字符串分隔
>> str='asdfg' >> '-'.join(str) 'a-s-d-f-g'
字符串格式化輸出
python字符串格式化輸出的三種方式
- 使用字符串格式格式化操作符——百分號(hào)%
- 使用字符串方法 format
- 使用 f-strings進(jìn)行字符串格式化
使用%進(jìn)行格式化
這種格式化表達(dá)式類似于C語言
格式化操作符(%) | 說明 |
---|---|
s | 獲取傳入對(duì)象的str方法的返回值,并將其格式化到指定位置 |
r | 與s一樣,但輸出方式是repr方式,而不是str |
c | 整數(shù):將數(shù)字轉(zhuǎn)換成其unicode對(duì)應(yīng)的值,10進(jìn)制范圍為 0<=i<=1114111(py27則只支持0-255);字符:將字符添加到指定位置 |
d | 有符號(hào)十進(jìn)制(整數(shù)),將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成十 進(jìn)制表示,并將其格式化到指定位置 |
i | 有符號(hào)整數(shù) |
u | 無符號(hào)整數(shù) |
o | 將整數(shù)轉(zhuǎn)換成八 進(jìn)制表示,并將其格式化到指定位置 |
x | 將整數(shù)轉(zhuǎn)換成十六進(jìn)制表示,并將其格式化到指定位置 |
X | 與x一樣,A-F是大寫 |
e | 浮點(diǎn)指數(shù),將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成科學(xué)計(jì)數(shù)法,并將其格式化到指定位置(小寫e) |
E | 與e一樣,E為大寫 |
f | 將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成浮點(diǎn)數(shù)表示,并將其格式化到指定位置(默認(rèn)保留小數(shù)點(diǎn)后6位) |
F | 浮點(diǎn)數(shù)十進(jìn)制 |
g | 浮點(diǎn)e或f,自動(dòng)調(diào)整將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成 浮點(diǎn)型或科學(xué)計(jì)數(shù)法表示 |
G | 浮點(diǎn)E或F,自動(dòng)調(diào)整將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成 浮點(diǎn)型或科學(xué)計(jì)數(shù)法表示 |
% | 當(dāng)字符串中存在格式化標(biāo)志時(shí),需要用 %%表示一個(gè)百分號(hào) |
注:Python中百分號(hào)格式化是不存在自動(dòng)將整數(shù)轉(zhuǎn)換成二進(jìn)制表示的方式
舉例
>>> "%s|%r|%c" %("this is str","this is repr","C")
"this is str|'this is repr'|C"
>>> "%d|%i|%o|%x|%X|" %(3,5,12,13,14)
'3|5|14|d|E|'
>>> "%e|%E|%f|%F|%g|%G" %(1.5E3,1.5e3,13.5,13.5,1.5e13,13.5E15)
'1.500000e+03|1.500000E+03|13.500000|13.500000|1.5e+13|1.35E+16'
>>> "%(string)-10s"%({'string':'1'})
'1
>>> "%(float)+10.2F"%({'float':3.1})
' +3.10'
>>> "%(float)-10.2f"%({'float':3.1})
'3.10 '
使用format方法
語法:{}.format(value)
參數(shù):
(value):可以是整數(shù),浮點(diǎn)數(shù),字符串,字符甚至變量。
Returntype:返回一個(gè)格式化字符串,其值在占位符位置作為參數(shù)傳遞。
#位置參數(shù)
>>> username='wanger'
>>> password=123456
>>> print ("{}'s password is {}".format(username,password))
wanger's password is 123456
>>> username='wanger'
>>> password=123456
>>> print ("{1}'s password is {0}".format(password,username))
wanger's password is 123456
#下標(biāo)參數(shù)
>>> si=['KB','MB','GB','TB','PB','EB','ZB','YB']
>>> '1000{0[0]}=1{0[1]}'.format(si)
'1000KB=1MB'
#浮點(diǎn)數(shù)精度
>>> '{:.4f}'.format(3.1415926)
'3.1416'
>>> '{:>10.4f}'.format(3.1415926)
' 3.1416'
>>> 'this is a test {t[0]}'.format(t='hello')
'this is a test h'
>>> 'this is a test {t[1]}'.format(t='hello')
'this is a test e'
#使用模塊作為參數(shù)
>>> import sys
>>> sys.platform
'win32'
>>> "{0.platform}".format(sys)
'win32'
>>> 'my laptop platform is {s}'.format(s=sys.platform)
'my laptop platform is win32'
>>> 'my laptop platform is (s.platform)'.format(s=sys)
'my laptop platform is (s.platform)'
#關(guān)鍵字參數(shù)
>>> 'my name is {name} ,age is {age}'.format(name='wanger',age='25')
'my name is wanger ,age is 25
當(dāng)占位符{}為空時(shí),Python將按順序替換通過str.format()傳遞的值。
str.format()方法中存在的值本質(zhì)上是元組數(shù)據(jù)類型,元組中包含的每個(gè)單獨(dú)值都可以通過索引號(hào)調(diào)用,索引號(hào)以索引號(hào)0開頭。
第三段代碼的變量si是一個(gè)列表,{0}就代表format()方法的第一個(gè)參數(shù),那么{0[0]}就代表列表的第一個(gè)元素,{0[1]}就代表列表的第二個(gè)元素
這個(gè)例子說明格式說明符可以通過利用(類似) Python 的語法
訪問到對(duì)象的元素或?qū)傩浴_@就叫做復(fù)合字段名 (compound field names) 。
以下復(fù)合字段名都是“ 有效的 ” 。
? 使用列表作為參數(shù),并且通過下標(biāo)索引來訪問其元素(跟上
一例類似)
? 使用字典作為參數(shù),并且通過鍵來訪問其值
? 使用模塊作為參數(shù),并且通過名字來訪問其變量及函數(shù)
? 使用類的實(shí)例作為參數(shù),并且通過名字來訪問其方法和屬性
? 以上方法的任意組合
表達(dá)式:format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]
- fill ::= <'any character'>
- align ::= "<"'左對(duì)齊' | ">"‘右對(duì)齊’ | "="‘在數(shù)字里,符號(hào)左對(duì)齊,數(shù)字右對(duì)齊 | "^"‘居中’
- sign ::= "+" | "-" | " "(當(dāng)sign=’+‘時(shí),即使是正數(shù)也會(huì)顯示符號(hào),-只有為負(fù)數(shù)的時(shí)候才顯示負(fù)號(hào),為空格時(shí),會(huì)在正數(shù)前面留下符號(hào)位)
- width ::= integer (定義輸出的寬度)
fill和align以及后面的width相當(dāng)于str方法中的center,ljust,rjust
>>> '{:+^15}'.format('start')
'+++++start+++++'
>>> '{:+^15}'.format('end')
'++++++end++++++'
>>> '{:*<15}'.format('end')
'end************'
>>> '{:*>15}'.format('start')
'**********start'
>>> '{:=+20}'.format(10)
'+ 10'
>>> print("{:=10}\n{:=+20}\n{:-^10}\n{:=-13}".format(10,10,'-',-15))
10
+ 10
----------
- 15
- #只有在數(shù)字顯示里,顯示二進(jìn)制數(shù),八進(jìn)制數(shù),十六進(jìn)制數(shù)的時(shí)候,需要顯示前面的0b,0o,0x的時(shí)候才會(huì)用到
>>> "{0:8b},{0:8o},{0:8x}".format(10) ' 1010, 12, a' >>> "{0:b},{0:o},{0:x}".format(10) '1010,12,a' >>> ("{0:#8b},{0:#8o},{0:#8x}".format(10)) ' 0b1010, 0o12, 0xa'
- ,符號(hào)是表示數(shù)字時(shí)每三位中間加,
>>> '{:,}'.format(100000000000) '100,000,000,000'
- 0是固定寬度前面補(bǔ)0
- .precision ::= integer(精度顯示)
>>> '{:010.5}'.format(3.1415926) '00003.1416'
- type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%" (跟之前使用%表示的相等)
??- 當(dāng)為字符時(shí):使用s,默認(rèn)就是s
??- 當(dāng)為整數(shù)時(shí):b,o,x和X是二進(jìn)制、八進(jìn)制、十六進(jìn)制,c是數(shù)字按Unicode轉(zhuǎn)換成字符,d是正常十進(jìn)制,默認(rèn)就是d。也可以使用n來代替d>>> "{0:d},{0:b},{0:o},{0:x},{0:X}".format(10) '10,1010,12,a,A'
??????- 為浮點(diǎn)數(shù)時(shí):e和E是指數(shù),f和F是浮點(diǎn)數(shù)。g和G是同一的,也可以使用n來代替g, %是顯示百分比
>>> "{0:e},{0:F},{0:g},{0:n},{0:%}".format(10.3) '1.030000e+01,10.300000,10.3,10.3,1030.000000%'
使用f-strings方法進(jìn)行格式化
f-strings也稱為“格式化字符串文字”,f字符串是f在開頭有一個(gè)字符串文字,其中以 {} 包含的表達(dá)式會(huì)進(jìn)行值替換。表達(dá)式在運(yùn)行時(shí)進(jìn)行評(píng)估,然后使用format協(xié)議進(jìn)行格式化。其中以 {} 包含的表達(dá)式會(huì)進(jìn)行值替換。
- 代碼簡(jiǎn)潔,沒有多余的引號(hào)
- 括號(hào){}里面的變量,可以是字符串類型,也可以是整型、浮點(diǎn)型,或者是復(fù)雜類型,比如數(shù)組、詞典等,會(huì)自動(dòng)轉(zhuǎn)換成成字符串形式。
- 括號(hào){}里面還可以是函數(shù),比如 f'數(shù)組a的長(zhǎng)度為:{len(a)}'。一句話,只要是位于 {} 中的,都會(huì)當(dāng)做 python 代碼來執(zhí)行。但里面只能寫表達(dá)式,不能寫執(zhí)行語句如{a=2}之類的。
- f-string在本質(zhì)上并不是字符串常量,而是一個(gè)在運(yùn)行時(shí)運(yùn)算求值的表達(dá)式,速度非???/li>
簡(jiǎn)單舉例
>>> name='wanger'
>>> age=25
>>> f"hello,I'm {name},my age {age} "
"hello,I'm wanger,my age 25 "
#也可以使用大寫F
>>> F"hello, I'm {name},my age {age} "
"hello, I'm wanger,my age 25 "
當(dāng)然也可以進(jìn)行簡(jiǎn)單的計(jì)算
>>> f"{2*3}"
'6'
也可以調(diào)用函數(shù)
>>> def test(input):
... return input.lower()
...
>>> name='WangEr'
>>> f"{test(name)} is funny"
'wanger is funny'
還可以選擇直接調(diào)用方法
>>> name='WangEr'
>>> f"{name.lower()} is funny"
'wanger is funny'
在使用字典的時(shí)候。如果要為字典的鍵使用單引號(hào),請(qǐng)記住確保對(duì)包含鍵的f字符串使用雙引號(hào)。
comedian = {'name': 'wanger', 'age': 25}
f"The comedian is {comedian['name']}, aged {comedian['age']}."
'The comedian is wanger, aged 25.'
使用字符串的場(chǎng)景
使用多個(gè)界定符分隔字符串
split只能使用單一字符串,如果要使用多個(gè)分隔符的話,就要用到正則表達(dá)式模塊了>>> str='asd,dfg;zxc ert uio' >>> import re >>> re.split(r'[;,\s]\s*',str) ['asd', 'dfg', 'zxc', 'ert', 'uio']
[]表示里面字符任意匹配。[;, ]表示;或者,或者空格,\s*表示任意個(gè)前面字符
- 字符串開頭或結(jié)尾匹配
比如要看一個(gè)地址是否是http://或者ftp://開頭
或者查看文件后綴是不是TXT格式
可以這樣查看>>> url='http://www.baidu.com' >>> ftp='ftp://www.baidu.com' >>> url.startswith(('http://','ftp://')) True >>> txt='ziyuan.txt' >>> txt.endswith('txt') True >>> url[0:7]=="http://" or url[0:6]=="ftp://" True >>> txt[7:10]=="txt" True
- 用shell通配符
我們還可以使用shell通配符來檢查文件的結(jié)尾,這需要用到fnmatch模塊
fnmatch不區(qū)分大小寫,fnmatchcase是區(qū)分大小寫的>>> from fnmatch import fnmatch,fnmatchcase >>> fnmatch('log.txt','*.txt') True >>> fnmatch('log.TXT','*.txt') True >>> fnmatchcase('log.TXT','*.txt') False >>> fnmatchcase('log.TXT','*.TXT') True
匹配和搜索特定格式的文本
普通匹配可以使用find方法,如果是特定格式的話還是會(huì)用到正則模塊>>> date1='2018/10/24' >>> date2='2018/12/21' >>> date3='2018-12-05' >>> def isdate(date): ... if re.match(r'\d+/\d+/\d+',date): ... print ('match OK') ... else: ... print ('not match') >>> isdate(date1) match OK >>> isdate(date2) match OK >>> isdate(date3) not match
在正則模塊re中\(zhòng)d表示單個(gè)數(shù)字,+表示一個(gè)或多個(gè)前面的字段
搜索和替換特定的文本格式
普通的匹配可以使用replace方法,如果匹配特定格式,還是要用正則模塊re>>> import re >>> date='today is 13/12/2018' >>> re.sub(r'(\d+)/(\d+)/(\d+)',r'\3,-\2-\1',date) 'today is 2018-12-13' >>> datepat=re.compile(r'(\d+)/(\d+)/(\d+)') #為了防止每次都要定義匹配模式,可以在這里定義一個(gè)匹配的變量,以后匹配直接調(diào)用這個(gè)變量 >>> datepat.sub(r'\3,-\2-\1',date) 'today is 2018-12-13' >>> date='yestory is 12/12/2018,today is 13/12/2018' >>> datepat.subn(r'\3,-\2-\1',date) ('yestory is 2018-12-12,today is 2018-12-13', 2)
\1,\2,\3分別代表前面匹配模式中的第一個(gè)括號(hào)匹配到的,第二個(gè)括號(hào)匹配到的,第三個(gè)括號(hào)匹配到的,使用subn方法可以看到匹配到幾次
忽略大小寫的搜索替換
如果要忽略大小寫還是要用到re模塊,需要用到的是re的IGNORECASE方法>>> import re >>> re.findall('replace','Replace,replace,REPLACE') ['replace'] >>> re.findall('replace','Replace,replace,REPLACE',flags=re.IGNORECASE) ['Replace', 'replace', 'REPLACE'] >>> str='Replace is the same as REPLACE' >>> re.sub('replace','WORD',str,flags=re.IGNORECASE) 'WORD is the same as WORD'
- 最短匹配模式
用正則表達(dá)式匹配某個(gè)文本模式,而他找到的是最長(zhǎng)匹配,如果要匹配最短字符,可以用下面的方法>>> strpat=re.compile(r'\"(.*)\"') >>> text='this is my "name"' >>> strpat.findall(text) ['name'] >>> text='this is my "name" and this is my "age"' >>> strpat.findall(text) ['name" and this is my "age'] >>> strpat=re.compile(r'\"(.*?)"') >>> strpat.findall(text) ['name', 'age']
歡迎各位關(guān)注本人微信公眾號(hào),“沒有故事的陳師傅”
當(dāng)前標(biāo)題:python數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)字和字符串
分享地址:http://www.dlmjj.cn/article/pggjhh.html