我有一串字节,我试图在 re:
user_name = 'Simon'
string = 'Hello {user_name}, nice to see you! :)'
然而,除了使用字节之外,re 字符串应该是原始字符串 ( r
)。
那么如何同时指定字节、f 字符串和原始字符串呢?
我试过了:
user_name = rb'Simon'
string = brf'Hello {user_name}, nice to see you! :)'
但:
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]:
我也试过format()
,但失败了:
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]:
如何指定多个字符串文字?