0

I want to display a text. and in the text there is a variable name, and the name of the variable is already in the previous definition.

example code

my_dict = {}
my_dict['name']='Candra'

#I want to be like this
print('my name is {name}')

#not like this
print('my name is '+ my_dict['name'])
  • 1
    The closest you can get is `print('my name is {name}'.format(name=my_dict['name']))`. – janos Nov 25 '17 at 06:45

2 Answers2

0

One way of doing this :

print "my name is {}".format(my_dict['name'])

Output : my name is Sandra

Other ways : print "my name is %s" % my_dict['name']

Shivansh Jagga
  • 1,541
  • 1
  • 15
  • 24
0

you could do this easily with python 3

my_dict = {}
my_dict['name']='Candra'
name = my_dict['name']


a = f"He said his name is {name}."
print(a) 

output would be

He said his name is Candra.
Adeojo Emmanuel IMM
  • 2,104
  • 1
  • 19
  • 28