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
  • 1
    The closest you can get is print('my name is {name}'.format(name=my_dict['name'])). Commented Nov 25, 2017 at 6:45

2 Answers 2

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']

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.