mkpisk
mkpisk

Reputation: 152

How to call a list in a string?

I am a new to python programming and trying to understand how it works while editing a string file. I wanted to call either variables or lists or tuple in a string and solve the values and update the string file. Here is a simple example


t_list = ['c','d','e']


doc = '''
 domain ()
 :types a b c - objects
        f"{t_list}" - items
'''
doc_up = doc

I wanted my doc_up to be updated with values of list t_list. I referred to PEP 498: Formatted string literals but it doesn't work.

My output is like this:

'\n domain ()\n :types a b c - objects\n        f"{t_list}" - items\n'

I want my output to be like this:

 domain ()
 :types a b c - objects
        c d e - items

Upvotes: 1

Views: 1010

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195418

You can use str.format. Remove f"..." from the string and leave just {t_list}, for example:

t_list = ["c", "d", "e"]


doc = """
 domain ()
 :types a b c - objects
        {t_list} - items
"""

doc_up = doc.format(t_list=" ".join(t_list))
print(doc_up)

Prints:


 domain ()
 :types a b c - objects
        c d e - items

Upvotes: 1

Related Questions