Generate Python String MD5 Value for Python Beginners – Python Web Crawler Tutorial

By | July 17, 2019

The md5 value of a string is very useful when you plan to save a string into a database, which can be used as a key primary. In this tutorial, we will introduce how to use python to generate the md5 value of a python string.

python generate md5 string

Preliminaries

#load model
#-*- coding: UTF-8 -*- 
import hashlib

Define a function to generate the md5 of string

def getMD5(str):
    # must encode
    str = str.encode("utf-8")
    hash_obj = hashlib.md5(str)
    return hash_obj.hexdigest()

Generate string md5

s = 'https://www.tutorialexample.com'
print(getMD5(s))

The md5 value is:

586882ad53f5782de25cf49026bc5a13

Notice:

Before getting md5 value of a string, you should encode it.

Leave a Reply