👻 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.
|
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:
|
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.
|
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:
- Italiano Python program to check if a string is palindrome or not
- Deutsch Python program to check if a string is palindrome or not
- Français Python program to check if a string is palindrome or not
- Español Python program to check if a string is palindrome or not
- Türk Python program to check if a string is palindrome or not
- Русский Python program to check if a string is palindrome or not
- Português Python program to check if a string is palindrome or not
- Polski Python program to check if a string is palindrome or not
- Nederlandse Python program to check if a string is palindrome or not
- 中文 Python program to check if a string is palindrome or not
- 한국어 Python program to check if a string is palindrome or not
- 日本語 Python program to check if a string is palindrome or not
- हिन्दी Python program to check if a string is palindrome or not
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
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
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