Converting hex to binary in the form of string This code returns a string of binary in 4-digit forms for a given hex in the form of string
def str_bin_in_4digits(aString):
    retStr = ''
    for i in aString:
        retStr = retStr+&quot;{0:04b}&quot;.format(int(i, 16))+&quot; &quot;
    return retStr.strip()

for example,
&gt;&gt;&gt; str_bin_in_4digits(&quot;20AC&quot;)
0010 0000 1010 1100

The code works as expected and my concern is could it be more elegant, like faster or less memory consumption?
Review
You don't have a lot of code here to review, so this will necessarily be short.

PEP-8: The Style Guide for Python Code recommends:

snake_case for functions, variables, and parameters.  So aString should be a_string, and retVal should be ret_val.


Better parameter names

What is aString?  &quot;Hello World&quot; is a string, but we can't use it, because you are actually expecting a hexadecimal string.  Perhaps hex_string would be a better parameter name.
Similarly, binary_string would be more descriptive than retStr.


A '''docstring''' would be useful for the function.
Type hints would also be useful.

Alternate Implementation
Doing things character-by-character is inefficient.  It is usually much faster to let Python do the work itself with its efficient, optimized, native code functions.
Python strings formatting supports adding a comma separator between thousand groups.
&gt;&gt;&gt; f&quot;{123456789:,d}&quot;
'123,456,789'

It also supports adding underscores between groups of 4 digits when using the binary or hexadecimal format codes:
&gt;&gt;&gt; f&quot;{548151468:_x}&quot;
'20ac_20ac'
&gt;&gt;&gt; f&quot;{0x20AC:_b}&quot;
'10_0000_1010_1100'

That is most of the way to what you're looking for.  Just need to turn underscores to spaces, with .replace(...) and fill with leading zeros by adding the width and 0-fill flag to the format string.
&gt;&gt;&gt; f&quot;{0x20AC:019_b}&quot;.replace('_', ' ')
'0010 0000 1010 1100'

A function using this technique could look like:
def str_bin_in_4digits(hex_string: str) -&gt; str:
    &quot;&quot;&quot;
    Turn a hex string into a binary string.
    In the output string, binary digits are space separated in groups of 4.

    &gt;&gt;&gt; str_bin_in_4digits('20AC')
    '0010 0000 1010 1100'
    &quot;&quot;&quot;

    value = int(hex_string, 16)
    width = len(hex_string) * 5 - 1
    bin_string = f&quot;{value:0{width}_b}&quot;
    return bin_string.replace('_', ' ')

if __name__ == '__main__':
    import doctest
    doctest.testmod(verbose=True)

Depending on your definition of elegant, you can one-line this:
def str_bin_in_4digits(hex_string: str) -&gt; str:
    &quot;&quot;&quot;
    Turn a hex string into a binary string.
    In the output string, binary digits are space separated in groups of 4.

    &gt;&gt;&gt; str_bin_in_4digits('20AC')
    '0010 0000 1010 1100'
    &quot;&quot;&quot;

    return f&quot;{int(hex_string,16):0{len(hex_string)*5-1}_b}&quot;.replace('_', ' ')