Junior — Middle
How can you determine if a string is a palindrome using Python?
sobes.tech AI
Answer from AI
A palindrome is a string that reads the same forwards and backwards. In Python, you can check this by comparing the string with its reversed version:
s = "level"
if s == s[::-1]:
print("The string is a palindrome")
else:
print("The string is not a palindrome")
Here, s[::-1] creates a reversed version of the string. If it matches the original, then the string is a palindrome.