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:
延伸文章資訊
- 1Python String encode() decode() - DigitalOcean
This function returns the bytes object. If we don't provide encoding, “utf-8” encoding is used as...
- 2Unicode HOWTO — Python 3.10.7 documentation
- 3Python3 bytes.decode()方法 - 菜鸟教程
Python3 bytes.decode()方法Python3 字符串描述decode() 方法以指定的编码格式解码bytes 对象。默认编码为'utf-8'。 语法decode()方法语法: ...
- 4Unicode HOWTO — Python 3.10.7 documentation
UTF-8 is one of the most commonly used encodings, and Python often defaults to ... UnicodeDecodeE...
- 5Python String encode() decode() - DigitalOcean