pythonstringcurly-bracesformatted

How can I print curly braces with this style formatted text in python


The following codes -

d = {'name':'Joe',
     'age': 25
    }

mypara ='''
My name is {name}.
   - I am {age} year old.
'''

print(mypara.format(**d))

gives the following output:

My name is Joe.
   - I am 25 year old.

How can I get output like below:

My name is {Joe}.
   - I am {25} year old.

The following works, but I'm looking for using the dictionary instead of variables -

name = 'Joe'
age = 25

mypara = f'''
My name is {{{name}}}.
   - I am {{{age}}} year old.
'''

print(mypara)

Output:

My name is {Joe}.
   I am {52} year old.

Solution

  • This works:

    d = {'name':'Joe', 'age': 25}
    
    my_para = f'''
    My name is {{{d['name']}}}.
       - I am {{{d['age']}}} years old.
    '''
    
    print(my_para)
    

    Is there any reason why you’re using a multiline string?