Python String encode() decode() - DigitalOcean
文章推薦指數: 80 %
This function returns the bytes object. If we don't provide encoding, “utf-8” encoding is used as default. Python Bytes decode(). Python bytes ...
Getalertedwhenassetsaredown,slow,orvulnerabletoSSLattacks—allfreeforamonth.Learnmore->We'rehiringBlogDocsGetSupportSalesProductsFeaturedProductsDropletsScalablevirtualmachinesKubernetesManagedKubernetesclustersAppPlatformGetappstomarketfasterFunctionsServerlesscomputesolutionDatabasesWorry-freesetup&maintenanceComputeDropletsKubernetesAppPlatformFunctionsStorageSpacesObjectStorageVolumesBlockStorageNetworkingVirtualPrivateCloud(VPC)CloudFirewallsLoadBalancersReservedIPsDNSManagedDatabasesMongoDBMySQLPostgreSQLRedis™ToolsandServicesAPICLISupportPlansTerraformProviderContainerManagementToolsMonitoringUptimeTeamsSeeallproductsSolutionsTryDigitalOceanforfreeClickheretosignupandget$200ofcredittotryourproductsover60days!SignupOurbusinesssolutionsWebsiteHostingVPSHostingByusecaseCloudVPNWeb&MobileAppsGameDevelopmentVideoStreamingSaaSPlatformsBlockchainQuestions?SpeakWithAnExpertTodaySeeallsolutionsMarketplaceCommunityOurcommunityCommunityHomeDevOpsanddevelopmentguidesCSS-TricksAllthingswebdesignResourcesTutorialsQuestionsAndAnswersToolsWriteforDOnationsCustomerStoriesDigitalOceanBlogGetInvolvedHatchStartupProgramOpenSourceSponsorshipsHacktoberfestDeployDOImpactDocumentationQuickStartDropletsStorageAppPlatformAPIReferenceDomainsandDNSPricingLoginSigninCommunityDigitalOceanSignupSignupCommunityDigitalOceanWe'rehiringBlogDocsGetSupportSalesTutorialsQuestionsTechTalksLearningPathsProductDocsSocialImpactSearchCommunityCONTENTSPythonStringencode()PythonBytesdecode()RelatedPythontimesleep()ViewVectorsinPython-AQuickIntroduction!View//Tutorial//PythonStringencode()decode()PublishedonAugust3,2022PythonPythonStringByPankajDeveloperandauthoratDigitalOcean.Whilewebelievethatthiscontentbenefitsourcommunity,wehavenotyetthoroughlyreviewedit.Ifyouhaveanysuggestionsforimprovements,pleaseletusknowbyclickingthe“reportanissue“buttonatthebottomofthetutorial.PythonStringencode()
Pythonstringencode()functionisusedtoencodethestringusingtheprovidedencoding.Thisfunctionreturnsthebytesobject.Ifwedon’tprovideencoding,“utf-8”encodingisusedasdefault.
PythonBytesdecode()
Pythonbytesdecode()functionisusedtoconvertbytestostringobject.Boththesefunctionsallowustospecifytheerrorhandlingschemetouseforencoding/decodingerrors.Thedefaultis‘strict’meaningthatencodingerrorsraiseaUnicodeEncodeError.Someotherpossiblevaluesare‘ignore’,‘replace’and‘xmlcharrefreplace’.Let’slookatasimpleexampleofpythonstringencode()decode()functions.
str_original='Hello'
bytes_encoded=str_original.encode(encoding='utf-8')
print(type(bytes_encoded))
str_decoded=bytes_encoded.decode()
print(type(str_decoded))
print('Encodedbytes=',bytes_encoded)
print('DecodedString=',str_decoded)
print('str_originalequalsstr_decoded=',str_original==str_decoded)
Output:
延伸文章資訊
- 1Decode UTF-8 in Python | Delft Stack
- 2Python String encode() decode() - DigitalOcean
This function returns the bytes object. If we don't provide encoding, “utf-8” encoding is used as...
- 3Unicode HOWTO — Python 3.10.7 documentation
- 4Day27 Python 基礎- 字符轉編碼操作 - iT 邦幫忙
首先通過編碼 decode 轉換為 Unicode 編碼 · 然後通過解碼 encode 轉換為 UTF-8 編碼.
- 5Python 3 Tutorial 第二堂(1)Unicode 支援、基本I/O
decode('UTF-8')) # 這是什麼? 原始碼檔案的編碼. 首先,程式一開始使用 input 函式指定提示訊息,這會讓程式停 ...