Home > Software engineering >  add each element in **quotes** to string
add each element in **quotes** to string

Time:09-29

I'm trying to solve this

my_list = ["foo", "bar", "baz"]

my_string = " They call me "

I would like to iterate in the list and print each element of the list in quotes at the end of the string.

something like this:

They call me "foo"
They call me "bar"
They call me "baz"

thank you

CodePudding user response:

Use the backslash '\' to escape double quotes.

my_string = " They call me \"" my_list[i] "\""

CodePudding user response:

you can miss f-strings

my_list = ["foo", "bar", "baz"]
my_string = " They call me "
for i in my_list:
    print(f'{my_string}"{i}"')

CodePudding user response:

Here's one solution.

for s in my_list:
    print(f"They call me \"{s}\"")

Alternatively, to use the my_string variable,

for s in my_list:
    print(f"{my_string}\"{s}\"")
  • Related