how to insert a variable into a string python

43

>>> name = "Fred"
>>> f"He said his name is {name}."
'He said his name is Fred.'
#Pre 3.6
print('Hi, my name is {name} and my age is {age}'.format(**locals()))

#After 3.6
print('Hi, my name is {name} and my age is {age}')
>>> line = 'Kong Panda'
>>> index = line.find('Panda')
>>> output_line = line[:index] + 'Fu ' + line[index:]
>>> output_line
'Kong Fu Panda'

Comments

Submit
0 Comments