Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Encoding and Decoding Custom Objects in Python-JSON
Next article icon

Encoding and Decoding Base64 Strings in Python

Last Updated : 07 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Base64 encoding is used to convert bytes that have binary or text data into ASCII characters. Encoding prevents the data from getting corrupted when it is transferred or processed through a text-only system. In this article, we will discuss about Base64 encoding and decoding and its uses to encode and decode binary and text data.

Base64 encoding:

It is a type of conversion of bytes to ASCII characters. the list of available Base64 characters are mentioned below:

  • 26 uppercase letters
  • 26 lowercase letters
  • 10 numbers
  • + and / for new lines

Each Base64 character represents 6 bits of data. it is also important to note that it is not meant for encryption for obvious reasons. To convert a string into a Base64 character the following steps should be followed:

  • Get the ASCII value of each character in the string.
  • Compute the 8-bit binary equivalent of the ASCII values
  • Convert the 8-bit characters chunk into chunks of 6 bits by re-grouping the digits
  • Convert the 6-bit binary groups to their respective decimal values.
  • 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.

Image Source: Wikipedia

Using python to encode strings:

In Python the base64 module is used to encode and decode data. First, the strings are converted into byte-like objects and then encoded using the base64 module. The below example shows the implementation of encoding strings isn’t base64 characters.

Example:

Python3 1==
import base64  sample_string = "GeeksForGeeks is the best" sample_string_bytes = sample_string.encode("ascii")  base64_bytes = base64.b64encode(sample_string_bytes) base64_string = base64_bytes.decode("ascii")  print(f"Encoded string: {base64_string}") 

Output:

Encoded string: R2Vla3NGb3JHZWVrcyBpcyB0aGUgYmVzdA==

Using Python to decode strings:

Decoding Base64 string is exactly opposite to that of encoding. First we convert the Base64 strings into unencoded data bytes followed by conversion into bytes-like object into a string. The below example depicts the decoding of the above example encode string output.

Example:

Python3 1==
import base64   base64_string =" R2Vla3NGb3JHZWVrcyBpcyB0aGUgYmVzdA ==" base64_bytes = base64_string.encode("ascii")  sample_string_bytes = base64.b64decode(base64_bytes) sample_string = sample_string_bytes.decode("ascii")  print(f"Decoded string: {sample_string}") 

Output:

Decoded string: GeeksForGeeks is the best


Next Article
Encoding and Decoding Custom Objects in Python-JSON

R

RajuKumar19
Improve
Article Tags :
  • Python
  • python-string
Practice Tags :
  • python

Similar Reads

  • base64.decodestring(s) in Python
    With the help of base64.decodestring(s) method, we can decode the binary string with the help of base64 data into normal form. Syntax : base64.decodestring(b_string) Return : Return the decoded string. Example #1 : In this example we can see that by using base64.decodestring(s) method, we are able t
    1 min read
  • How Base64 Encoding and Decoding is Done in Node.js ?
    Base64 encoding and decoding are when you need to encode binary data into a text format that can be easily transmitted over text-based protocols like HTTP. Node.js provides built-in methods for Base64 encoding and decoding, which can be easily performed using the Buffer class. Table of Content Appro
    3 min read
  • base64.encodestring(s) in Python
    With the help of base64.encodestring(s) method, we can encode the string using base64 encoded data into the binary form. Syntax : base64.encodestring(string) Return : Return the encoded string. Example #1 : In this example we can see that by using base64.encodestring(s) method, we are able to get th
    1 min read
  • Encoding and Decoding Custom Objects in Python-JSON
    JSON as we know stands for JavaScript Object Notation. It is a lightweight data-interchange format and has become the most popular medium of exchanging data over the web. The reason behind its popularity is that it is both human-readable and easy for machines to parse and generate. Also, it's the mo
    5 min read
  • How to Encrypt and Decrypt Strings in Python?
    In this article, we will learn about Encryption, Decryption and implement them with Python.  Encryption: Encryption is the process of encoding the data. i.e converting plain text into ciphertext. This conversion is done with a key called an encryption key. Decryption: Decryption is the process of de
    6 min read
  • base64.decodebytes(s) in Python
    With the help of base64.decodebytes(s) method, we can decode the binary string with the help of base64 data into normal form. Syntax : base64.decodebytes(b_string) Return : Return the decoded string. Example #1 : In this example we can see that by using base64.decodebytes(s) method, we are able to g
    1 min read
  • Python Strings decode() method
    The decode() method in Python is used to convert encoded text back into its original string format. It works as the opposite of encode() method, which converts a string into a specific encoding format. For example, let's try to encode and decode a simple text message: [GFGTABS] Python s = "Geek
    4 min read
  • Python | C Strings of Doubtful Encoding | Set-2
    String handling in extension modules is a problem. C strings in extensions might not follow the strict Unicode encoding/decoding rules that Python normally expects. Thus, it’s possible that some malformed C data would pass to Python. A good example might be C strings associated with low-level system
    3 min read
  • Python | C Strings of Doubtful Encoding | Set-1
    One can convert strings between C and Python vice-versa but the C encoding is of a doubtful or unknown nature. Let's suppose that a given C data is supposed to be UTF-8, but it’s not being strictly enforced. So, it is important to handle such kind of malformed data so that it doesn’t crash Python or
    2 min read
  • base64.encodebytes(s) in Python
    With the help of base64.encodebytes(s) method, we can encode the string using base64 encoded data into the binary form. Syntax : base64.encodebytes(string) Return : Return the encoded string. Example #1 : In this example we can see that by using base64.encodebytes(s) method, we are able to get the e
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences