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:
延伸文章資訊
- 1Understand the encoding/decoding of Python strings (Unicode ...
It's obvious that in Python 3 a string is by default a Unicode string and the u'' prefix is optio...
- 2How to decode a UTF-8-encoded byte string in Python
Call bytes.decode(encoding) with encoding as "utf8" to decode a UTF-8-encoded byte string bytes .
- 3Decode UTF-8 in Python | Delft Stack
- 4Python String encode() decode() - DigitalOcean
- 5Remove Unicode Characters In Python