0

is there not a way to make this work in python, to compare or compute if a string is a palindrome i know why it doesn't work, but is there some python magic to make python read the other side of the index like -1(end to start) instead of 1(start to end)

 for x in string:
    if x == -x:return True

am not looking for slicing / reversing the string using function and then comparing !

Knotwood V
  • 51
  • 1
  • 6

3 Answers3

1

Use string slicing:

if string == string[::-1]:
    return True
Jakob F
  • 1,046
  • 10
  • 23
0

Strings don't support the minus operator but you can use subscripting to obtain what you're looking for:

if x == x[::-1]:  ...
Alain T.
  • 40,517
  • 4
  • 31
  • 51
  • yes , but x refers to a range in for-loop so it there not away to x to start at the end of the range i want to take x and make it -(x) – Knotwood V Feb 10 '20 at 18:30
0
    if str(string)[::-1] == str(string):
    return True
User
  • 61
  • 1
  • 13