Home > database >  How can i print on new lines?
How can i print on new lines?

Time:02-06

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

CodePudding user response:

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))

CodePudding user response:

Convert boolean to string, then insert newlines.

print("\n".join(map(str, palindrome_status)))

CodePudding user response:

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')

CodePudding user response:

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

  • Related