Home » Check if a string is a palindrome in python

Check if a string is a palindrome in python

1 Year Subscription
Java SE 11 Developer (Upgrade) [1Z0-817]
Spring Framework Basics Video Course
Java SE 11 Programmer II [1Z0-816] Practice Tests
Oracle Java Certification
Java SE 11 Programmer I [1Z0-815] Practice Tests

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