Remember to import base64 and that the b64encode function takes bytes as an argument.

import base64
b = base64.b64encode(bytes('your_string', 'utf-8')) # bytes
base64_str = b.decode('utf-8') # convert bytes to string

Explanation:

The bytes function creates a bytes object from the string "your_string" using UTF-8 encoding. In Python, bytes represents a sequence of bits and UTF-8 specifies the character encoding to use.

The base64.b64encode function encodes bytes object into Base64 format. It takes a bytes-like object as input and returns a Base64 encoded bytes object.

The b.decode function decodes the bytes object (here b) using UTF-8 encoding and returns the resulting string. It converts the bytes back to their original string representation.

Answer from Alexander Ejbekov on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ encoding-and-decoding-base64-strings-in-python
Encoding and Decoding Base64 Strings in Python - GeeksforGeeks
August 7, 2024 - Use the Base64 encoding table to align the respective Base64 values for each decimal value. The below image provides us with a Base64 encoding table. ... In Python the base64 module is used to encode and decode data.
Discussions

How to encode text to base64 in python - Stack Overflow
I am trying to encode a text string to base64. ... Copyname = "your name" print('encoding %s in base64 yields = %s\n'%(name,name.encode('base64','strict'))) ... Take a look at this answer. base64 is binary-to-text encoding I don't know why python's base64 module doesn't convert it back to string ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Base64 encode of image too long
issue solved when updating to 14.6rc - base64 on image files is working again properly. More on reddit.com
๐ŸŒ r/shortcuts
5
3
May 17, 2021
Converting string to mp4 video file
This is too vague for any meaningful answer. More on reddit.com
๐ŸŒ r/learnpython
10
0
March 2, 2021
Is Base64 encoded UUID still unique?

base64 encoding does not shorten the data, it makes it longer. It converts 3 bytes of data into 4 readable characters. It is not lossy, you can convert it back to the original bytes with a base64 decode.

If the original bytes were unique, the base64 string should be unique as well.

Edit -- maybe I'm misunderstanding the question? If you are truncating the data, you won't guarantee uniqueness anymore. Depending on the level of truncations, collisions might be rare, but they'll still be possible.

More on reddit.com
๐ŸŒ r/javahelp
4
2
September 3, 2019
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ base64.html
base64 โ€” Base16, Base32, Base64, Base85 Data Encodings
The legacy interface does not support decoding from strings, but it does provide functions for encoding and decoding to and from file objects. It only supports the Base64 standard alphabet, and it adds newlines every 76 characters as per RFC 2045.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ encoding-and-decoding-base64-strings-in-python
Encoding and Decoding Base64 Strings in Python
September 4, 2023 - Without using 'rb', Python would assume we are reading a text file. We then use the read() method to get all the data in the file into the binary_file_data variable. Similar to how we treated strings, we Base64 encoded the bytes with base64.b64encode and then used the decode('utf-8') on base64_encoded_data to get the Base64 encoded data using human-readable characters.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-base64-encode
Python Base64 Encode | b64encode() Function Guide
February 1, 2024 - We then define a byte string data that we want to encode. We pass this data to the b64encode() function, which returns the base64 encoded version of the data. This encoded data is then printed out.
๐ŸŒ
Medium
medium.com โ€บ @ume.cooray โ€บ understanding-base64-encoding-in-python-5b31f23dd007
Understanding Base64 Encoding and Decoding in Python | by Umayangana Cooray | Medium
March 18, 2025 - In Python, strings need to be converted to bytes before encoding. This is done using .encode('utf-8'). This ensures the string is represented properly in binary form. ... base64.b64encode() takes the byte data and encodes it into Base64.
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ How-do-you-encode-strings-to-Base64-in-Python
How to encode strings to Base64 in Python - Quora
Answer (1 of 4): Use the base64 module, which is part of the Python standard library. The methods there take โ€œbytes-like objectsโ€, so you will most likely have to create a [code ]bytes[/code] object from your string, specifying which encoding to use. Hereโ€™s an example: [code]>>> ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_module_base64.asp
Python base64 Module
The base64 module encodes binary data into ASCII strings using Base16, Base32, Base64, and related encodings.
๐ŸŒ
Python Module of the Week
pymotw.com โ€บ 2 โ€บ base64
base64 โ€“ Encode binary data into ASCII characters - Python Module of the Week
import base64 original_string = ...ded_string) print 'Decoded :', decoded_string ยท The encoding process looks at each sequence of 24 bits in the input (3 bytes) and encodes those same 24 bits spread over 4 bytes in the output....
๐ŸŒ
Base64 Encode
base64encode.net โ€บ python-base64-b64encode
Python base64.b64encode() - Base64 Encode
<<< import base64 <<< encoded = base64.b64encode('base64 encoded string') <<< encoded 'YmFzZTY0IGVuY29kZWQgc3RyaW5n'
๐ŸŒ
Base64encoder
base64encoder.io โ€บ python
https://www.base64encoder.io/python/
We cannot provide a description for this page right now
๐ŸŒ
DEV Community
dev.to โ€บ atifwattoo โ€บ what-is-base64-string-in-python-4joo
What is base64 string in Python? - DEV Community
December 23, 2024 - import base64 # Encode a string to base64 data = "Hello, World!" encoded = base64.b64encode(data.encode()) print(encoded) # Output: b'SGVsbG8sIFdvcmxkIQ==' # Decode a base64 string decoded = base64.b64decode(encoded).decode() print(decoded) # Output: "Hello, World!"
๐ŸŒ
Base64.Guru
base64.guru โ€บ home โ€บ developers โ€บ python
Python Base64 Encode | Python | Developers | Base64
To encode data in Base64, Python provides the b64encode function of the base64 module. In addition, this module also provides the b64decode function, as well as other useful functions to encode binary into text using several algorithms specified in RFC 3548. b64encode(data) - Encodes data to ...
๐ŸŒ
DEV Community
dev.to โ€บ tooleroid โ€บ base64-encoding-in-python-simplified-guide-examples-nan
Base64 Encoding in Python: Simplified Guide & Examples - DEV Community
December 16, 2024 - A: Use regex or try-except blocks with b64decode to validate Base64 strings. Q: What's the performance impact of Base64 encoding? A: Base64 encoding/decoding has minimal CPU impact but increases data size by about 33%. Q: How do I handle Base64 in Python web frameworks?
๐ŸŒ
Sling Academy
slingacademy.com โ€บ article โ€บ how-to-base64-encode-decode-a-string-in-python
How to base64 encode/decode a string in Python - Sling Academy
July 1, 2023 - Import the base64 module. Encode the string to bytes using the appropriate encoding (e.g., UTF-8).
๐ŸŒ
Rip Tutorial
riptutorial.com โ€บ encoding and decoding base64
Python Language Tutorial => Encoding and Decoding Base64
import base64 s = "Hello World!" b = s.encode("UTF-8") e = base64.b64encode(b) print(e) ... which is still in the bytes object. To get a string out of these bytes, we can use Python's decode() method with the UTF-8 encoding:
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-use-base64b64encode-in-python
How to use base64.b64encode() in Python
The program obtains the binary form of the string pppfoo??? through utf-8 encoding. The program then encodes the bytes in base64 and specifies in the altchars argument that / or + is encoded with :.
๐ŸŒ
Cybrosys Technologies
cybrosys.com โ€บ odoo blogs
Base64 Encoding & Decoding Using Python
The Python base64 module provides an easy way to encrypt and decrypt binary data using the Base64 encoding technique. It is also extremely simple to use.
๐ŸŒ
Bit Level Code
bitlevelcode.com โ€บ home โ€บ python โ€บ encoding and decoding using base64 strings in python
Encoding and Decoding Using Base64 Strings in Python
May 12, 2025 - The Python Module Base64 provides functionalities for encoding and decoding. You first need to convert the string into bytes to encode a string to base 64 in Python, then use the base64 method to encode these bytes.