learnedlizard
learnedlizard

Reputation: 13

How do I make the output printed in separate lines?

i have this code

import random
sen_1 = input("pls enter the sent 1: ") 
sen_2 = input("pls enter the sent 2: ")
sen_3 = input("pls enter the sent 3: ")
sen_4 = input("pls enter the sent 4: ")

sent = [sen_1 , sen_2 , sen_3 , sen_4]
random.shuffle(sent)
print(sent)

instead of ["x", "x", "x", "x"]

Im expecting an output like this:

["x", 

 "x",

 "x",

 "x"]

Thanks in advance

Upvotes: 1

Views: 103

Answers (4)

jottbe
jottbe

Reputation: 4521

You could also simply join the contents of the list with the newline character

print('\n'.join(sent))

Upvotes: 0

d-xa
d-xa

Reputation: 524

To obtain the exact output as you described, I would first call the str() method then replace the "," with a ",\n" (comma-separator and a line-break), like so

print(str(sent).replace("," , ",\n"))

So your print output will look exactly like this:

['x',
 'x',
 'x',
 'x']

Upvotes: 1

alx
alx

Reputation: 844

You can also use this:

print(*sent, sep = "\n") 

Finally, it will look like that:

import random
sen_1 = input("pls enter the sent 1: ") 
sen_2 = input("pls enter the sent 2: ")
sen_3 = input("pls enter the sent 3: ")
sen_4 = input("pls enter the sent 4: ")

sent = [sen_1 , sen_2 , sen_3 , sen_4]
random.shuffle(sent)
print(*sent, sep = "\n")

Upvotes: 3

Yolan Maldonado
Yolan Maldonado

Reputation: 88

You need to loop your array like this:

for element in sent:
    print(element)

Upvotes: 0

Related Questions