Python program to check if a string is palindrome or not

| | | | | | | | | | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

Examples :

  Input:  malayalam  Output:  Yes  Input:  geeks  Output:  No 

Method # 1
1) Find Backward Line
2) Check if the reverse and the original match.

# function that returns the back string

def reverse (s):

return s [:: - 1 ]

def isPalindrome (s):

# Call back function

rev = reverse (s)

# Check whether both strings are equal or not

if (s = = rev):

return True

return False


# Driver code

s = "malayalam"

ans = isPalindrome (s)

if ans = = 1 :

print ( " Yes " )

else :

print ( " No " )

Output:

 Yes 

Iterative Method: this method is provided by Ball Raza . Loop from start to length / 2 and check the first character to the last character of the string and the second to the second last, and so on .... If any character does not match, the string will not be a palindrome.

Below is the implementation of the above approach:

# function to check the line
# palindrome or not

def isPalindrome ( str ):

# Loop from 0 to len / 2

for i in xrange ( 0 , len ( str ) / 2 ):

if str [i]! = str [ len ( str ) - i - 1 ]:

return False

return True


# main function

s = "malayalam"

ans = isPalindrome (s)

if (ans):

print ( "Yes" )

else :

print ( "No" )

Output:

 Yes 

Method using built-in function for call lines: this method is provided by Shariq Raza . In this method, the predefined function & # 39; & # 39; .join (reversed (string)) is used to reversed a string.

Below is the implementation of the above approach:

# function to check the string
# palindrome or not

def isPalindrome (s):

# Using a predefined function for

# revert to line output

rev = ’’ .join ( reversed ( s))

# Check if both lines are

# equal or not

if (s = = rev):

return True

return False


# main function

s = "malayalam"

ans = isPalindrome (s)

if (ans):

print ( "Yes" )

else :

print ( "No" )

Output:

 Yes 

Method using one additional variable: In this method, the user takes a string character one by one and stores it in an empty variable. After saving all characters, the user compares both strings and checks if it is a palindrome or not.

# Python program to check
# if the string is a palindrome
# or not

x = "malayalam"

w = ""

for i in x:

w = i + w

if (x = = w):

print ( " YES " )

Exit :

 Yes 

This article is courtesy of Sahil Rajput . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.

👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from Python program to check if a string is palindrome or not, check other __main__ Python module-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Walter Wu

Massachussetts | 2023-03-26

I was preparing for my coding interview, thanks for clarifying this - Python program to check if a string is palindrome or not in Python is not the simplest one. I am just not quite sure it is the best method

Julia Wu

New York | 2023-03-26

Maybe there are another answers? What Python program to check if a string is palindrome or not exactly means?. Will use it in my bachelor thesis

Javier Porretti

San Francisco | 2023-03-26

Thanks for explaining! I was stuck with Python program to check if a string is palindrome or not for some hours, finally got it done 🤗. I am just not quite sure it is the best method

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically