python3對文字和二進位資料做了區分。文字是Unicode編碼,str類型,用於顯示。二進位類型是bytes類型,用於儲存和傳輸。 bytes是byte的序列,而str是unicode的序列。本文主要介紹了python string類型 bytes類型 bytearray類型,需要的朋友可以參考下,希望能幫助到大家。
一、str型別:
>>> s = u'你好' >>> s '你好' >>> type(s) <class></class>
bytes型別:
##
>>> b = b'abc' >>> b b'abc' >>> type(b) <class></class>
##二、str與bytes之間的轉換關係:str-->encode()-->bytes-->decode()-->str
轉換方式一:encode( ),decode()
>>> a = u'你好' >>> b = a.encode('utf-8') >>> b b'\xe4\xbd\xa0\xe5\xa5\xbd' >>> type(b) <class> >>> new_a = b.decode('utf-8') >>> new_a '你好' >>> type(new_a) <class></class></class>
轉換方式二:bytes(),str()
>>> a = u'你好' >>> b= bytes(a, encoding='utf-8') >>> b b'\xe4\xbd\xa0\xe5\xa5\xbd' >>> type(b) <class> >>> new_a = str(b, encoding='utf-8') >>> new_a '你好' >>> type(new_a) <class></class></class>
#三、bytearray型別
bytearray類別是range 0
可選的來源參數可以用幾種不同的方式來初始化陣列:
•如果它是一個字串,那麼您還必須給出編碼(以及可選的錯誤)參數;bytearray()然後使用str.encode()將字串轉換為位元組。
•如果它是一個整數,那麼陣列將具有這個大小,並將用null位元組初始化。•如果它是符合緩衝區介面的對象,則將使用對象的唯讀緩衝區來初始化位元組數組。
•如果它是可迭代的,那麼它必須是range 0 •如果沒有參數,則創建一個大小為0的數組。當來源參數是字串時:
>>> b = bytearray(u'你好', encoding='utf-8') >>> b bytearray(b'\xe4\xbd\xa0\xe5\xa5\xbd') >>> type(b) <class></class>
#當來源參數是整數時:
>>> b = bytearray(5) >>> b bytearray(b'\x00\x00\x00\x00\x00') >>> type(b) <class></class>
當來源參數是可迭代對象,那麼這個迭代對象的元素都必須符合0
>>> b = bytearray([1, 2, 3, 4, 255]) >>> b bytearray(b'\x01\x02\x03\x04\xff') >>> type(b) <class><p></p> <p>四、bytes和bytearray區別<a href="http://www.php.cn/python-tutorials-382623.html" target="_self"></a>bytes是不可變的,同str。 bytearray是可變的,同list。 </p> <p><a href="http://www.php.cn/python-tutorials-382157.html" target="_self"><pre class="brush:php;toolbar:false"> >>> b = bytearray() >>> b bytearray(b'') >>> b.append(10) >>> b bytearray(b'\n') >>> b.append(100) >>> b bytearray(b'\nd') >>> b.remove(100) >>> b bytearray(b'\n') >>> b.insert(0, 150) >>> b bytearray(b'\x96\n') >>> b.extend([1, 3, 5]) >>> b bytearray(b'\x96\n\x01\x03\x05') >>> b.pop(2) 1 >>> b bytearray(b'\x96\n\x03\x05') >>> b.reverse() >>> b bytearray(b'\x05\x03\n\x96') >>> b.clear() >>> b bytearray(b'')
>>> b = b'abcdef' >>> bay = bytearray(b) >>> bay bytearray(b'abcdef') >>> b = bytes(bay) >>> b b'abcdef'#########六、bytearray和str轉換#########
>>> a = 'abcdef' >>> b = bytearray(a, encoding='utf-8') >>> b bytearray(b'abcdef') >>> a = b.decode(encoding='utf-8') >>> a 'abcdef'#########相關推薦:##########淺聊numpy數組的幾個排序方式_python######## ####用Python語言描述最大連續子序列和############完全掌握Python######
以上是python string、 bytes、 bytearray類型詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

concatenateListSinpythonWithTheSamelements,使用:1)operatoTotakeEpduplicates,2)asettoremavelemavphicates,or3)listcompreanspherensionforcontroloverduplicates,每個methodhasdhasdifferentperferentperferentperforentperforentperforentperfornceandordorimplications。

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允許ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

WebStorm Mac版
好用的JavaScript開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具