javascriptpythonbase64language-comparisons

Does python have an equivalent to Javascript's 'btoa'


I'm trying to find an exact equivalent to javascript's function 'btoa', as I want to encode a password as base64. It appears that there are many options however, as listed here:

https://docs.python.org/3.4/library/base64.html

Is there an exact equivalent to 'btoa' in python?


Solution

  • Python's Base64:

    import base64
    
    encoded = base64.b64encode(b'Hello World!')
    print(encoded)
    
    # value of encoded is SGVsbG8gV29ybGQh
    

    Javascript's btoa:

    var str = "Hello World!";
    var enc = window.btoa(str);
    
    var res = enc;
    
    // value of res is SGVsbG8gV29ybGQh
    

    As you can see they both produce the same result.