How can i print on new lines?

Question:

How can i print my output from this function and each boolean to be on new line.

def is_palindrome(n):
    return str(n) == str(n)[::-1]


numbers = list(map(int, input().split(', ')))
palindrome_status = [is_palindrome(n) for n in numbers]

print(palindrome_status)

Output:

[False, True, False, True]

Expecting:

False
True
False
True
Asked By: Kpuc

||

Answers:

Convert boolean to string, then insert newlines.

print("n".join(map(str, palindrome_status)))
Answered By: J_H

The simplest would be print it one by one:

[print(is_palindrome(n)) for n in numbers]

BUT

List comprehesion shouldn’t be used with side effect functions, to have it clean you should use normal loop:

for n in numbers:
    print(is_palindrome(n))
Answered By: kosciej16

There are two options:

  1. Use a for loop to iterate over the elements and then print

for val in palindrome_status:
print(val)

  1. Use the print separator for newlines while unpacking the list

print(*palindrome_status, sep=’n’)

Answered By: A-T

This works with Lua sometimes if you do

print( "string1" ..

"string2")

using "..", it tells it to continue as one function, and it will continue with this until it’s closed or ended

I’m not a python guy so, Sorry if it doesnt work in python :C

Answered By: J1mster
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.