string to base64 bytes, exactly with the same literal chars
<p>today I got stucked with the following issue. I can explain it better with the inverse purpose: Say we have the following base64-variable,</p>

<pre><code>b64_var = b'aGVsbG8gd29ybGQ='
</code></pre>

<p>Imagine that what we want it's not to decode it into string object of python, namely,</p>

<pre><code>base64.b64decode(b64_var).decode('utf-8')
&gt;&gt;&gt; 'hello world'
</code></pre>

<p>but the literal equivalent characters in string instead,</p>

<pre><code>repr( b64_var )[1:]
&gt;&gt;&gt; 'aGVsbG8gd29ybGQ='
</code></pre>

<p>Ok, now it's my question. I want to build a function that performs just the opposite,</p>

<pre><code>'aGVsbG8gd29ybGQ=' -&gt; b'aGVsbG8gd29ybGQ=' (base64-obj) ???
</code></pre>

<p>I got stucked, I don't know how to do it.. Any suggestion? Thank you in advance.</p>

<p>Ok, I found the problem, it was easier as I expected.</p>

<p>First I am going to try to clarify what actually I wanted to do,</p>

<pre><code>str_var = 'aGVsbG8gd29ybGQ='               # str_var: string  
b64_var = fun_str_2_b64samechars(str_var)  # b64_var: bytes (b64)
</code></pre>

<p>Console,</p>

<pre><code>b64_var
&gt;&gt;&gt;b'aGVsbG8gd29ybGQ='
</code></pre>

<p>My goal was to guess how to build that <code>fun_str_2_b64samechars</code> function. And here it is exactly <code>bytes(str_var,'utf-8')</code>,</p>

<pre><code>bytes('aGVsbG8gd29ybGQ=','utf-8')
&gt;&gt;&gt;b'aGVsbG8gd29ybGQ='
</code></pre>

<p>Probably it was because I thought it was some kind of base64 class as long as I employed the base64 module (due to this I edited the title). Thanks for the help and I hope it could be useful for someone :)</p>

python string python-3.x base64 literals