Python字符串解码()方法

decode()是Python 2中字符串中指定的方法。 此方法用于将参数字符串编码为所需编码方案的一种编码方案转换为所需编码方案。这与编码相反。它接受编码字符串的编码以对其进行解码,并返回原始字符串。

null

语法: 解码(编码,错误)

参数: 编码: 指定解码必须基于的编码。 错误: 决定发生错误时如何处理,例如“strict”在出现异常时引发Unicode错误,“ignore”忽略发生的错误。

返回: 返回编码字符串中的原始字符串。

代码#1: 解码字符串的代码

# Python code to demonstrate
# decode()
# initializing string
str = "geeksforgeeks"
# encoding string
str_enc = str .encode(encodeing = 'utf8' )
# printing the encoded string
print ( "The encoded string in base64 format is : " ,)
print (str_enc )
# printing the original decoded string
print ( "The decoded string is : " ,)
print (str_enc.decode( 'utf8' , 'strict' ))


输出:

The encoded string in base64 format is :  Z2Vla3Nmb3JnZWVrcw==

The decoded string is :  geeksforgeeks

申请: 编码和解码可以一起用于将密码存储在后端的简单应用程序,以及许多其他应用程序,如处理保密信息的密码学。 下面描述了密码应用程序的一个小演示。

代码#2: 用于演示编解码应用程序的代码

# Python code to demonstrate
# application of encode-decode
# input from user
# user = input()
# pass = input()
user = "geeksforgeeks"
passw = "i_lv_coding"
# converting password to base64 encoding
passw = passw.encode( 'base64' , 'strict' )
# input from user
# user_login = input()
# pass_login = input()
user_login = "geeksforgeeks"
# wrongly entered password
pass_wrong = "geeksforgeeks"
print ( "Password entered : " + pass_wrong )
if (pass_wrong = = passw.decode( 'base64' , 'strict' )):
print ( "You are logged in !!" )
else : print ( "Wrong Password !!" )
print ( '
'
)
# correctly entered password
pass_right = "i_lv_coding"
print ( "Password entered : " + pass_right )
if (pass_right = = passw.decode( 'base64' , 'strict' )):
print ( "You are logged in !!" )
else :
print ( "Wrong Password !!" )


输出:

Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享