(ii) To check whether a string named 'word' is a palindrome or not.

Answer:

Answer by student

The python statement to check whether a string named, word is a palindrome or not is:

 word == word[::-1]

Detailed answer by teachoo

-lock-

  • To check whether a string named word is a palindrome or not, we can use the slicing operator ([:]) of the string object.
  • The slicing operator takes three arguments: start, stop, and step, and returns a substring of the original string from start to stop with a step size.
  • If start is omitted, it defaults to 0. If stop is omitted, it defaults to the length of the string. If step is omitted, it defaults to 1. If step is negative, it reverses the direction of slicing.
  • Therefore, we can write word[::-1] to get the reverse of word by using a negative step size of -1.
  • Then we can compare word with its reverse using the equality operator (==), which returns True if they are equal and False otherwise.
  • Therefore, we can write word == word[::-1] to check if word is a palindrome or not.

So, the correct answer is word == word[::-1]

-endlock-

Remove Ads
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.