简体   繁体   中英

Encode the string “tamilnadu” in python 3

I want to encode the string "tamilnadu" and the output is like "dGFtaWxuYWR1"

str = "tamilnadu";
print str.encode('base64','strict') //dGFtaWxuYWR1

But when I am encode the string its shows an error like this

'base64' is not a text encoding; use codecs.encode() to handle arbitrary codecs

I tired all the encoding techniques, but the output for the sting is not like this encode value "dGFtaWxuYWR1"

How can I encode the string in python 3.4

You need base64 module

import base64
base64.b64encode(bytes('tamilnadu', 'utf-8'))

Also, don't use str as a variable name - its a keyword in python which represents string type.

Use base64 library

    import base64
    c="tamilnadu"
    c=base64.b64encode(bytes(c, 'utf-8')) // b'dGFtaWxuYWR1'
    c1=base64.b64decode(c) 
    c=str(c).replace("'","")  // bdGFtaWxuYWR1
    print("Encode",c[1:])       // dGFtaWxuYWR1 
    c1=str(c1).replace("'","") // b'tamilnadu'
    print("Decode=",c1[:1]) // tamilnadu

After encode you have replace the ' and first character b

You need base64 module

import base64
base64.b64encode(bytes('tamilnadu', 'utf-8'))

Also, don't use str as a variable name - its a keyword in python which represents string type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM