Home > Enterprise >  How to insert variable value to already existing string python?
How to insert variable value to already existing string python?

Time:03-30

How to Insert variable value to string python? I need to insert 'g' value to {g} in html text. code

tried f string and it doesn't work

CodePudding user response:

You can use str.format to insert value into {g} in your example

g = 234

text = '''
<p>Hi {g}!</p>
'''

print(text.format(g=g))

CodePudding user response:

Using string replacement, but this is not super save...

htmlmsg.replace('{g}', str(g))
  • Related