← How-To Guides How-To How to hash a string Use hashlib: hashlib.sha256(s.encode()).hexdigest(). Use SHA-256 or better for security. SHA-256 hexdigest() returns a hex string. import hashlib h = hashlib.sha256(b'secret').hexdigest()