Search code examples
pythonstringbyterawstring

How to use multiple string literals at once


I have a string of bytes that I am trying to use in an re:

user_name = 'Simon'
string = 'Hello {user_name}, nice to see you! :)'

However as well as using bytes an re string should be a raw string (r).

So how can I specify bytes, f-strings and raw strings at once?

I tried:

user_name = rb'Simon'
string = brf'Hello {user_name}, nice to see you! :)'

But:

In [1]: user_name = rb'Simon'
   ...: string = brf'Hello {user_name}, nice to see you! :)'
  File "<ipython-input-8-93fb315cc66f>", line 2
    string = brf'Hello {user_name}, nice to see you! :)'
                                                       ^
SyntaxError: invalid syntax


In [2]:

I also tried format() but that failed:

In [2]: string = br'Hello {}, nice to see you! :)'.format(user_name)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-36faa39ba31d> in <module>()
----> 1 string = br'Hello {}, nice to see you! :)'.format(user_name)

AttributeError: 'bytes' object has no attribute 'format'

In [3]:

How can specify multiple string literals?


Solution

  • String interpolation is limited to strings and can't be used on bytes. This is addressed in the PEP:

    For the same reason that we don't support bytes.format(), you may not combine 'f' with 'b' string literals. The primary problem is that an object's __format__() method may return Unicode data that is not compatible with a bytes string.


    The workaround is to manually encode the string to bytes:

    >>> rf'Hello {user_name}, nice to see you! :)'.encode()
    b'Hello Simon, nice to see you! :)'