Search code examples
pythonpython-3.xformattingprintfstring.format

How to make python printf return this?


I need print(f"" + xx) to output text aaa but it outputs {x} how could i make this work?

I have tried with printf and .format but couln't make any of those to work

x = "aa"
xx = "text {x}"
x = "aaa"
print(f"" + xx)

Solution

  • I think you want to use str.format, not an f-string.

    xx = "text {x}"
    x = "aaa"
    print(xx.format(x=x))  # -> text aaa
    

    Or without passing x explicitly:

    print(xx.format(**locals()))  # -> text aaa