import sys
for line in sys.stdin:
if line == line[::-1]:
print('true')
else :
print('false')
I type abba
but got false
I print line[::-1] out it was 'abba'
why result is false?
import sys
for line in sys.stdin:
if line == line[::-1]:
print('true')
else :
print('false')
I type abba
but got false
I print line[::-1] out it was 'abba'
why result is false?
When you enter code in using stdin the string has a escape character \n which means new line. It is better to use input statement. If you want to keep it like that here is how you fix it.
for line in sys.stdin:
line = line.strip()
if line == line[::-1]:
print('True')
else:
print('False')
Basically spaces and \n is concatenating with your string variable that is why its not matching, use line.strip() function, this function will remove all the spaces in the string. import sys
for line in sys.stdin:
line = line.strip()
if line == line[::-1]:
print('true')
else :
print('false')