Where is 'palindrome' defined?
sohcahtoa82 at gmail.com
sohcahtoa82 at gmail.com
Mon Jun 1 21:07:16 EDT 2015
On Monday, June 1, 2015 at 5:55:14 PM UTC-7, fl wrote:
> On Sunday, May 31, 2015 at 9:46:56 PM UTC-7, fl wrote:
> > Hi,
> >
> > When I search solution of reverse a string/number, I came across a short
> > function online:
> >
> > >>> def palindrome(num):
> > return str(num) == str(num)[::-1]
> >
> > I thought that it is a general function. And with the following variable:
> >
> > >>> a
> > '1234_5555'
> >
> > >>> parlindrome(a)
> >
> > Traceback (most recent call last):
> > File "<pyshell#126>", line 1, in <module>
> > parlindrome(a)
> > NameError: name 'parlindrome' is not defined
> >
> >
> > Then, I find that parlindrome is a special checking mirrored word.
> > I use Python 2.7.9. Why does the error message show
> >
> > name 'parlindrome' is not defined
> >
> >
> >
> > Thanks,
>
> Thanks, I realize that it was spelled wrong. Now, with the correct spelling
> the result is a 'False'. I have expected it gives reversed string. Is the
> function behaves correct or not?
>
>
> Thanks,
>
>
>
>
> >>> a='1234'
> >>> def palindrome(num):
> return str(num) == str(num)[::-1]
> >>> palindrome(a)
> False
> >>> palindrome("fread")
> False
If you want the reversed string, then just use a[::-1]. Your palindrome function simply returns a boolean telling you whether or not the string you gave it is a palindrome.
More information about the Python-list
mailing list