MD5: Why am I getting different results for the same string?

Question:

I expected the following code to return the same result in each case since the string values are the same but instead got a different result each time. What can I do (if anything) to address this?

import hashlib

a = 'some text'
b = 'some text'
hashA = hashlib.md5(b'{a}').hexdigest()[:8]
hashB = hashlib.md5(b'{b}').hexdigest()[:8]
hashT = hashlib.md5(b'some text').hexdigest()[:8]

print(hashT) # 552e21cd
print(hashA) # e78fce13
print(hashB) # 09b94c63
print (a==b) # True
Asked By: Edward

||

Answers:

Because the prefix is f for formatted strings. a, b and ‘some text’ are different.

Answered By: Jonathan Dauwe
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.