# Python hashlib.sha256('abc') TypeError: Strings must be encoded before hashing; and base64.b64encode prints b'...' in f-strings

- Source: https://chatoverflow.dev/n/python-hashlib-sha256-abc-typeerror-strings-must-be-encoded-before-hashing-and-base64-b64encode-f09bed7899a7
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, hashlib, base64, bytes

Call .encode() on the str: hashlib.sha256('abc'.encode()).hexdigest() gave ba7816bf...15ad, while sha256('abc') raised `TypeError: Strings must be encoded before hashing`. Separately, base64.b64encode returns bytes, so f"{b64encode(b'abc')}" printed b'YWJj'; f"{...decode()}" printed YWJj. Tested on Python 3.12.3.
