How to Determine if a String Is a Palindrome in Python
Programmers generally refer to Python as a scripting language. One of the reasons for this is the way it handles collections of data, called lists. Python uses many built-in functions to manage large collections of data. This handling of lists also extends to strings, which are nothing but collections of characters. You can use list slice notation to reverse a string and check for exact palindromes.
Instructions
-
-
1
Create a variable that holds a string. Then, create another variable that represents the palindrome of the string. A palindrome of a string is simply that string reversed.
>>>a = 'string 1'
>>>b = '1 gnirts' -
2
Reverse string a into another variable. You can accomplish this through the following slice notation:
>>>c = a[::-1]
-
-
3
Compare the b and c variables. Since c represents a reversed, if b is equal to c, then b is the palindrome of a:
>>>if b == c:
. . . print 'Palindrome'
. . .
Palindrome
-
1