Home ยป Check if a string is a palindrome in python

Check if a string is a palindrome in python

In this example we check if a string is a palindrome in python

A palindrome is a string that is the same read forward or backward.

Example

// check if a string is palindrome or not
function checkPalindrome(string) 
{
    // convert string to an array
    const arrayValues = string.split('');

    // reverse the array
    const reversedArray = arrayValues.reverse();

    // convert array to string
    const reversedString = reversedArray.join('');

	//check both strings
    if(string == reversedString) 
	{
        console.log('It is a palindrome');
    }
    else 
	{
        console.log('It is not a palindrome');
    }
}

//get input
const string = prompt('Enter a string: ');

checkPalindrome(string);

When you run this you will see something like this

It is a palindrome

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More