Vincent Nguyen
Vincent Nguyen

Reputation: 21

I cannot get new line in python

my code:

question_prompts = [
    "What color are apples?]\n(a) Red/Green \n(b) Yellow \n(c) Purple\n\n",
]
print(question_prompts)

\n in this array [] didn't work. It cannot be down a new line. Hope to get your help

Upvotes: 2

Views: 332

Answers (5)

Amal K
Amal K

Reputation: 4854

It would be easy to work with if you use a dictionary for your options instead.

question = "What color are apples?"
options  = {'a': 'Red/Green', 'b': 'Yellow', 'c': 'Purple'}

print(question)
for option in "abcd":
 print(f'({option}) {options[option]}')

Upvotes: 0

eliottness
eliottness

Reputation: 135

That's because you are printing the list and not the string whose inside the list, try this:

question_prompts = [
    "What color are apples?]\n(a) Red/Green \n(b) Yellow \n(c) Purple\n\n",
]

for prompt in question_prompts:
    print(prompt)

Upvotes: 1

Prakash
Prakash

Reputation: 491

Since the string is inside a list, it does not print properly..

You can use

for q in question_prompts:
    print(q)

and you should see the newlines

Upvotes: 0

Gavin Wong
Gavin Wong

Reputation: 1260

You will need to print the items separately, as follows:

question_prompts = ["What color are apples?", "(a) Red / Green", "(b) Yellow", "(c) Purple"]

for item in question_prompts:
    print(item)
    

Upvotes: 0

user12867493
user12867493

Reputation:

The issue is you are printing the whole of question_prompts which just shows you what is in the whole of that array without getting a new line.

You need to specifically print the first element:

>>> print(question_prompts[0])
What color are apples?]
(a) Red/Green 
(b) Yellow 
(c) Purple

To print all the elements:

for prompt in question_prompts:
    print(prompt)

Upvotes: 0

Related Questions