How can I convert string variable into byte type in python? : <p>I want to encrypt a messages which I got from user input using Cryptography:</p>

<p>I have the following simple code:</p>

<pre><code>import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
backend = default_backend()
messages = input("Please, insert messages to encrypt: ")
key = os.urandom(24)
print(key)
cipher = Cipher(algorithms.TripleDES(key), modes.ECB(), backend=backend)
encryptor = cipher.encryptor()
cryptogram = encryptor.update(b"a secret message") + encryptor.finalize()
print(cryptogram)
</code></pre>

<p>When I hard code the messages <kbd>"a secret message"</kbd> with <kbd>b</kbd> prefix in the code it works fine.
What I wanted to do is to use <kbd>messages</kbd>variable to get the text from user input.</p>

<pre><code>messages = input("Please, insert messages to encrypt: ")
</code></pre>

<p>I've tried several ways to covert the string type from the input to byte type and pass it to encryptor.update method but nothing works for me.</p>

<pre><code>messages = input(b"Please, insert messages to encrypt: ")
cryptogram = encryptor.update(byte, message) + encryptor.finalize()
...
</code></pre>

<p>Versions:</p>

<p>Python 3.7.0</p>

<p>Cryptography 2.4</p>

<p>Mac OS</p>
