The problem is that you use
Inside f-string the
Can trick it together so it kind of work,but make it easier for yourself and don't use zero byte escape character as a replace.
\0
escape character that is a zero byte.Inside f-string the
r
want work then nothing get printed after zero byte.Can trick it together so it kind of work,but make it easier for yourself and don't use zero byte escape character as a replace.
import re string1 = 'Agent Cheesecake was a double agent.' agent_names_regex = re.compile(r'Agent (\w)\w*') mo_0 = agent_names_regex.sub(r'\0**', string1) print(fr"This is string1 '{string1}' with \0** after censorship: {mo_0!r}")
Output:This is string1 'Agent Cheesecake was a double agent.' with \0** after censorship: '\x00** was a double agent.'