I am new to Python and I would like some comments to the following code. It is for checking if a string is a palindrome (in one or two dimensions).
string_to_check = str(input("Please enter a phrase to check if it is palindrome: "))
string_to_check = string_to_check.upper() #transform the string to uppercase
list_to_check = string_to_check.split()
print ('The string to check is: \"{}" and has {} words'.format(string_to_check , number_of_words))
#two-dimension palindrome check - with two full lists
reversed_list_to_check = list(map(lambda x:x[::-1], list_to_check[::-1]))
if list_to_check == reversed_list_to_check :
two_dimension_palindrome = True
print ("The string %s is a two-dimension palindrome" %string_to_check)
else :
#one-dimension palindrome check
string_to_check = "".join(list_to_check) #string without spaces
if string_to_check == string_to_check[::-1]:
one_dimension_palindrome = True
print ('%s is a one-dimension palindrome' % string_to_check)
else:
print ("Your string is not a palindrome")
Is this the Pythonic way?
number_of_words
is not defined. You could consider removing punctuation. \$\endgroup\$