convert string to md5 in python

To convert a string to an MD5 hash in Python, you can use the hashlib module.

Here's an example code block:

main.py
import hashlib

my_string = "Hello World!"
hash_object = hashlib.md5(my_string.encode())
print(hash_object.hexdigest())
120 chars
6 lines

In this code, we first import the hashlib module. We then define a string - "Hello World!" - and create an MD5 hash object from it using hashlib.md5(). We encode the string using .encode() since the hashlib.md5() function requires a bytes-like object. Finally, we print the hex digest of the hash using .hexdigest().

gistlibby LogSnag