# How to find the longest Palindrome in a String using Python

This occasionally comes up during coding interviews and is actually quite a decent way to test someone’s aptitude of moving back and forth on a string to determine if and where palindromes exist.

If we simply said: “return a boolean if a string is a palindrome”, then threw a couple tests cases at the function, we would expect the developer to loop through the first half of a string while comparing the second half, if it matched all the way to the central pivot, then “yes, the string is a palindrome”.

However, this is a bit more complex.

## The problem statement

“Give a string `s`, find the longest palindromic substring in `s`.”

Example1:
Input: “lamar”
Output: “ama”

Example2:
Input: “cbbd”
Output: “bb”

Example3:
Input: “rotator”
Output: “rotator”

There may be multiple ways to achieve this, so coming up with a decent time and space answer is more ideal.

## How do we solve this?

We start by creating a variable to remember our palindrome (which we hopefully find).

Then we loop through the input string, followed by a second loop in reverse through the same string.

At this point we check both positions to determine their match and return once we’re done.

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```def longestPalindrome(s) -> str:
# Create a string to store our resultant palindrome
palindrome = ''

# loop through the input string
for i in range(len(s)):

# loop backwards through the input string
for j in range(len(s), i, -1):

# Break if out of range
if len(palindrome) >= j-i:
break

# Update variable if matches
elif s[i:j] == s[i:j][::-1]:
palindrome = s[i:j]
break

return palindrome
```Code language: Python (python)```
Subscribe
Notify of
1 Comment
Inline Feedbacks
View all comments

[…] This occasionally comes up during coding interviews and is actually quite a decent way to test someone’s aptitude of moving back and forth on a string to determine if and where palindromes exist. If we simply said: “return a boolean if a string is a palin… Read more […]