-1

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

grayred8
  • 93
  • 6
  • Does this answer your question? [Appending the same string to a list of strings in Python](https://stackoverflow.com/questions/2050637/appending-the-same-string-to-a-list-of-strings-in-python) – ThePyGuy Sep 29 '22 at 15:17
  • Does this answer your question? [Using quotation marks inside quotation marks](https://stackoverflow.com/questions/9050355/using-quotation-marks-inside-quotation-marks) – mkrieger1 Sep 29 '22 at 15:27

4 Answers4

2

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}\"")
Ben Grossmann
  • 4,387
  • 1
  • 12
  • 16
  • 1
    You can nest double quotes inside single quotes and the backslashes wouldn't be necessary. – Dennis Williamson Sep 29 '22 at 15:22
  • @DennisWilliamson I'm aware, but I find that this practice makes for confusing code. – Ben Grossmann Sep 29 '22 at 15:25
  • @BenGrossmann I see your reasoning although even the [Black code formatter](https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#strings), which converts all single quotes strings to double quoted ones, will convert them to single quoted if they contain a backslash double quote due to less special character usage. Which also results in a smaller line length And I agree with this reasoning – Jab Sep 29 '22 at 15:32
  • @Jab That interesting. Personally, I'm in the habit of using single-quotes for characters (that is, one-character strings) and double quotes in all other cases. – Ben Grossmann Sep 29 '22 at 15:37
  • 1
    @BenGrossmann It's because of things like this that make python difficult to understand or difficult to conform to it's code styling, it is so flexible that there's many ways to do things, it makes sense why many other languages (strongly typed ones in specific) have very specific ways of doing things. It makes reading code easier. – Jab Sep 29 '22 at 15:41
  • Thank you @BenGrossmann ! I'll go with the second solution – grayred8 Sep 29 '22 at 18:42
1

You can use f-strings.

my_list = ["foo", "bar", "baz"]
my_string = " They call me "
for i in my_list:
    print(f'{my_string}"{i}"')
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
jan
  • 56
  • 6
1

You may use raw strings, repr is the function used to convert a string to raw string.

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

my_string = "They call me "

for i in my_list:
    print(my_string,repr(i))

output:

They call me  'foo'
They call me  'bar'
They call me  'baz'
Faraaz Kurawle
  • 1,085
  • 6
  • 24
0

You can use double quotes inside with single quote outside in line 1.

my_list = ['"foo"', '"bar"', '"baz"']
my_string = "They call me "
for i in my_list:
    print(f'{my_string}{i}')
toyota Supra
  • 3,181
  • 4
  • 15
  • 19