Python Strings decode() method - GeeksforGeeks (2024)

Last Updated : 01 Jul, 2024

Summarize

Comments

Improve

In Python we have decode() is a method specified in Strings. This method is used to convert from one encoding scheme, in which the argument string is encoded to the desired encoding scheme. This works opposite to the encode. It accepts the encoding of the encoding string to decode it and returns the original string.

Python Decode() Function Syntax

Syntax:decode(encoding, error)
Parameters:

  • encoding : Specifies the encoding on the basis of which decoding has to be performed.
  • error : Decides how to handle the errors if they occur, e.g ‘strict’ raises Unicode error in case of exception and ‘ignore’ ignores the errors occurred.
  • Returns : Returns the original string from the encoded string.

Encode and Decode a String in Python

The above code is an example of encoding and decoding. Here first we encoded the string using UTF-8 and then decoded it which gives the same output String as we give it in input.

Python3
# initializing stringString = "geeksforgeeks"encoded_string = String.encode('utf-8')print('The encoded string in base64 format is :')print(encoded_string)decoded_string = encoded_string.decode('utf-8')print('The decoded string is :')print(decoded_string)

Output:


The encoded string in base64 format is :
b'geeksforgeeks'
The decoded string is :
geeksforgeeks

Application of Encode-Decode

Encoding and decoding together can be used in the simple applications of storing passwords in the back end and many other applications like cryptography which deals with keeping information confidential. A small demonstration of the password application is depicted below.

Python3
import base64user = "geeksforgeeks"passw = "i_lv_coding"# Converting password to base64 encodingpassw_encoded = base64.b64encode(passw.encode('utf-8')).decode('utf-8')user_login = "geeksforgeeks"# Wrongly entered passwordpass_wrong = "geeksforgeeks"print("Password entered:", pass_wrong)if pass_wrong == base64.b64decode(passw_encoded).decode('utf-8'): print("You are logged in!")else: print("Wrong Password!")print()# Correctly entered passwordpass_right = "i_lv_coding"print("Password entered:", pass_right)if pass_right == base64.b64decode(passw_encoded).decode('utf-8'): print("You are logged in!")else: print("Wrong Password!")

Output:

Password entered : geeksforgeeks
Wrong Password!!
Password entered : i_lv_coding
You are logged in!!

Working of the Python Decode() Method?

The following flowchart shows the working of Python decoding:

Python Strings decode() method - GeeksforGeeks (1)

Decode()

Python Strings decode() method – FAQs

How to use decode in Python 3?

In Python 3, the decode method is used to convert a bytes object into a str (string) object by decoding it from a specific encoding.

Example:

# Define a bytes object
bytes_obj = b'Hello, world!'

# Decode bytes object to string using UTF-8 encoding
string_obj = bytes_obj.decode('utf-8')

print(string_obj) # Output: Hello, world!

Here, decode('utf-8') converts the bytes object from UTF-8 encoding to a string.

What does .decode('utf-8') do?

The .decode('utf-8') method converts a bytes object into a str object using the UTF-8 encoding. UTF-8 is a variable-width character encoding used for text.

Example:

# Define a bytes object with UTF-8 encoding
bytes_obj = b'\xe2\x9c\x94'

# Decode bytes object to string
string_obj = bytes_obj.decode('utf-8')

print(string_obj) # Output: ✓

What is string decoding?

String decoding is the process of converting encoded bytes back into a string. It interprets bytes according to a specified character encoding to produce a readable string.

Example:

# Define a bytes object
bytes_obj = b'Hello'

# Decode bytes object to string
string_obj = bytes_obj.decode('utf-8')

print(string_obj) # Output: Hello

What is an example of decode?

Here’s a basic example of how to use the decode method with a bytes object:

Example:

# Define a bytes object
bytes_data = b'Hello, Python!'

# Decode bytes object to string using UTF-8 encoding
text = bytes_data.decode('utf-8')

print(text) # Output: Hello, Python!

What is encode() in Python?

The encode() method is used to convert a string into a bytes object using a specific encoding. This is the reverse of decode().

Example:

# Define a string
text = 'Hello, world!'

# Encode string to bytes using UTF-8 encoding
bytes_obj = text.encode('utf-8')

print(bytes_obj) # Output: b'Hello, world!'

Additional Example of encode():

# Define a string
text = 'Hello, Python!'

# Encode string to bytes
bytes_data = text.encode('utf-8')

print(bytes_data) # Output: b'Hello, Python!'



Python Strings decode() method - GeeksforGeeks (3)

Improve

Please Login to comment...

Python Strings decode() method - GeeksforGeeks (2024)
Top Articles
How Much Money Should You Keep In Your Savings Account?
Credit Scores Education Article
11 beste sites voor Word-labelsjablonen (2024) [GRATIS]
Is Sam's Club Plus worth it? What to know about the premium warehouse membership before you sign up
Instructional Resources
Mrh Forum
Wellcare Dual Align 129 (HMO D-SNP) - Hearing Aid Benefits | FreeHearingTest.org
Google Sites Classroom 6X
Google Jobs Denver
Gunshots, panic and then fury - BBC correspondent's account of Trump shooting
Ashlyn Peaks Bio
Jet Ski Rental Conneaut Lake Pa
Sitcoms Online Message Board
Helloid Worthington Login
Echo & the Bunnymen - Lips Like Sugar Lyrics
Connect U Of M Dearborn
Craftology East Peoria Il
Arre St Wv Srj
Tygodnik Polityka - Polityka.pl
Daylight Matt And Kim Lyrics
Jbf Wichita Falls
Food Universe Near Me Circular
Putin advierte que si se permite a Ucrania usar misiles de largo alcance, los países de la OTAN estarán en guerra con Rusia - BBC News Mundo
Little Rock Skipthegames
Ecampus Scps Login
Craigslist Pennsylvania Poconos
Finding Safety Data Sheets
Restaurants In Shelby Montana
Bayard Martensen
They Cloned Tyrone Showtimes Near Showbiz Cinemas - Kingwood
Greater Orangeburg
6465319333
Urban Blight Crossword Clue
Jambus - Definition, Beispiele, Merkmale, Wirkung
Southern Democrat vs. MAGA Republican: Why NC governor race is a defining contest for 2024
Covalen hiring Ai Annotator - Dutch , Finnish, Japanese , Polish , Swedish in Dublin, County Dublin, Ireland | LinkedIn
Missouri State Highway Patrol Will Utilize Acadis to Improve Curriculum and Testing Management
Metro By T Mobile Sign In
Rage Of Harrogath Bugged
Barber Gym Quantico Hours
Pay Entergy Bill
The Holdovers Showtimes Near Regal Huebner Oaks
1Exquisitetaste
The power of the NFL, its data, and the shift to CTV
Television Archive News Search Service
877-552-2666
Congruent Triangles Coloring Activity Dinosaur Answer Key
David Turner Evangelist Net Worth
Appsanywhere Mst
Vrca File Converter
Jasgotgass2
Obituary Roger Schaefer Update 2020
Latest Posts
Article information

Author: Manual Maggio

Last Updated:

Views: 5690

Rating: 4.9 / 5 (49 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Manual Maggio

Birthday: 1998-01-20

Address: 359 Kelvin Stream, Lake Eldonview, MT 33517-1242

Phone: +577037762465

Job: Product Hospitality Supervisor

Hobby: Gardening, Web surfing, Video gaming, Amateur radio, Flag Football, Reading, Table tennis

Introduction: My name is Manual Maggio, I am a thankful, tender, adventurous, delightful, fantastic, proud, graceful person who loves writing and wants to share my knowledge and understanding with you.