How do I turn a Python string stored in a variable into a byte sequence? -


very simple, know, docs aren't helpful. i'm trying hash simple string. following this guide. example given therein is:

import hashlib hash_object = hashlib.md5(b'hello world') print(hash_object.hexdigest()) 

and have hash representation. suppose want take 1 step further. have 4 strings want concatenate together, result of needs converted byte sequence, in order passed hashlib.md5() function. however, i'm curious how can replicate b'hello world' syntax using variable instead of hard-coded string. docs seem suggest can pass in format built-in format function, use-case like:

my_string = '%s%s%s%s' % (first, second, third, fourth) byte_string = format(my_string, 'b') 

this doesn't quite work, though. how do this?

strings in python sequence of characters, convert string sequence of bytes encode using character set. example:

my_string = '%s%s%s%s' % (first, second, third, fourth) byte_string = my_string.encode('utf-8') 

instead of my_string.encode('utf-8') use bytes(my_string, 'utf-8'), these equivalent. can use different encoding if like, utf-8 choice because capable of representing code point (character) , compact, ascii data.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -