Wolfram Language

Text & Language Processing

Palindromes

A palindrome is a number, word, or sentence that reads the same from left to right and right to left. Use PalindromeQ, its options, and other string-related low-level functions to test for palindromic words and sentences.

This is an English palindrome.

In[1]:=
Click for copyable input
PalindromeQ["racecar"]
Out[1]=

The name Anna is also a palindrome, but you need to ignore the capitalization.

In[2]:=
Click for copyable input
PalindromeQ["Anna", IgnoreCase -> True]
Out[2]=

Use Decapitalize as an alternative to make the first character be lowercase.

In[3]:=
Click for copyable input
PalindromeQ[Decapitalize["Anna"]]
Out[3]=

The French verb "rêver" is a palindrome as well, but you need to ignore the diacritic.

In[4]:=
Click for copyable input
PalindromeQ["rêver", IgnoreDiacritics -> True]
Out[4]=

Use RemoveDiacritics to process the verb instead.

In[5]:=
Click for copyable input
PalindromeQ[RemoveDiacritics["rêver"]]
Out[5]=

With palindromic sentences, you also generally need to process the initial string.

In[6]:=
Click for copyable input
sentence = "Now, sir, a war is won!";
In[7]:=
Click for copyable input
PalindromeQ[sentence]
Out[7]=
In[8]:=
Click for copyable input
StringDelete[sentence, PunctuationCharacter | Whitespace]
Out[8]=
In[9]:=
Click for copyable input
StringDelete[sentence, PunctuationCharacter | Whitespace]; PalindromeQ[%, IgnoreCase -> True]
Out[9]=

Related Examples

de es fr ja ko pt-br ru zh