-1
fno = input()
myList = list(fno)
sum = 0
for i in range(len(fno)):
    if myList[0:] == myList[:0]:
    continue
print (myList)

I want to make a number palindrome. eg:

input(123)
print(You are wrong)
input(12121)
print(you are right) 

Please Guide me how to make a palindrome in python.Its not complete code please suggest me what the next step.

Thanks

  • possible duplicate of [Python reverse() for palindromes](http://stackoverflow.com/questions/5202533/python-reverse-for-palindromes) – jamylak May 05 '12 at 10:19
  • What exactly do you mean by 'make a number a palindrome'? Give an example of what you expect to take in and get out. – Gareth Latty May 05 '12 at 10:24
  • 1
    Your example doesn't clarify much. You say 'make' but seem to show *checking* it to see if it's a palindrome. If you do mean checking as in your example, my answer works. – Gareth Latty May 05 '12 at 10:31

4 Answers4

6

I presume, given your code, you want to check for a palindrome, not make one.

There are a number of issues with your code, but in short, it can be reduced down to

word = input()
if word == "".join(reversed(word)):
    print("Palidrome")

Let's talk about your code, which doesn't make much sense:

fno = input() 
myList = list(fno) #fno will be a string, which is already a sequence, there is no need to make a list.
sum = 0 #This goes unused. What is it for?
for i in range(len(fno)): #You should never loop over a range of a length, just loop over the object itself.
    if myList[0:] == myList[:0]: #This checks if the slice from beginning to the end is equal to the slice from the beginning to the beginning (nothing) - this will only be true for an empty string.
        continue #And then it does nothing anyway. (I am presuming this was meant to be indented)
print (myList) #This will print the list of the characters from the string.
Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
  • @DrTyrsa That's not a good solution. Most importantly, it's cryptic to people who don't know the syntax, and it is also potentially slower. – Gareth Latty May 05 '12 at 10:23
  • 2
    Can you demonstrate where it is "potentially slower"? And I think we write code for those who know the syntax. – DrTyrsa May 05 '12 at 10:25
  • 2
    @DrTyrsa There is a reason the `reversed()` built in exists. Python aims to be readable, and `reversed(x)` is clearer than `x[::-1]`. As to slower, classes can provide `__reversed__()` to give faster methods of reversing their contents if there is a way to do it. – Gareth Latty May 05 '12 at 10:29
  • IMO using `[::-1]` in this case is fine. I would like this solution more if there was no need for `"".join`. It can be argued that `"".join` could be considered cryptic as well, it just seems like overkill to me. – jamylak May 05 '12 at 10:32
  • Remark about `__reversed__` is strange, there is just a string here, nothing more. And classes also provide `__getitem__` method, that can accept slice object as a key and you can place your "faster methods" there (and htere will be no need to define `__reversed__` btw). There is indeed a reason for the `reversed`, it works fine with iterators, not evaluating them. If you want to evaluate the iterator (and in the case of the question you do want), `[::-1]` is the most short and readable way to do it. – DrTyrsa May 05 '12 at 10:37
  • 1
    @DrTyrsa You can't use a slice on an iterator - only on sequences. Using `__getitem__` to provide a faster method of dealing with reversing is clumsy and awkward. `reversed()` is there for a reason. Yes, with strings it's not going to help, but in general it may. `reversed()` is readable, which should be your main concern. It's clear what you are doing. – Gareth Latty May 05 '12 at 10:39
  • What that's all about? There is no iterator here, just a string. If there were an iterator here, your solution would be wrong, as your `==` comparison would alsways be `False` (comparing an iterator with a string). – DrTyrsa May 05 '12 at 10:44
  • You said in your comment *'If you want to evaluate the iterator (and in the case of the question you do want), [::-1] is the most short and readable way to do it.'* - you can't do a slice (or `reversed()`) on an iterator. – Gareth Latty May 05 '12 at 10:48
  • 1
    @DrTyrsa He means that `reversed` only works on `sequences`. It can't work on `iterators` since it needs to iterate backwards and `iterators` start at the beginning and can only access each item in order. – jamylak May 05 '12 at 10:49
  • Ok, wanted to say "if you don't afraid to evaluate the iterable (and in the case of the question you don't)..." Anyway, `[::-1]` is a well-know idiom for every Python programmer, so it will be very readable for them. And this idiom is very short. Oh, and it solves the task. – DrTyrsa May 05 '12 at 10:55
  • @DrTyrsa One of the things about Python is that it should be obvious - not to Python programmers, not to programmers, but to everyone. `reversed()` is obvious. `[::-1]` is only well-known because everyone gets taught it, because it's not clear. People need to stop using it and use the better, clearer method. – Gareth Latty May 05 '12 at 11:02
  • "Python... should be obvious - not to Python programmers, not to programmers, but to everyone" - who said that? – DrTyrsa May 05 '12 at 11:06
  • @DrTyrsa The Zen of python talks about 'the obvious way to do things'. Look at the choices that have been made with Python - `/` being full division in 3.x, stuff like that. It's clear what is obvious to a programmer (that `/` is integer division) is not obvious in Python terms. One of the big aims of Python is that 'readability counts'. `reversed()` is the better option, for that reason, if no other. – Gareth Latty May 05 '12 at 11:12
  • 1
    "The obvious way to do thing" != "obvious... not to programmers, but to everyone". `[::-1]` is obvious to Python programmer. And you haven't showed the source of the words in quotes. – DrTyrsa May 05 '12 at 11:19
  • 1
    I said that - it wasn't a quote until you quoted it. If you are suggesting that it's simply stating 'it should be obvious to people that have learned the language' - well, that's true for all languages. – Gareth Latty May 05 '12 at 11:21
  • 1
    So, it's just your opinion. Can you show some real-life open source Python app (may be small-sized), which code is obvious not to programmers, but to everyone? – DrTyrsa May 05 '12 at 11:28
  • 2
    @DrTyrsa I'm not saying that an entire page of Python code should be obvious to a non-programmer, but rather, language constructs should try to make as much sense as possible to them. I'm not going to spam this further by continuing to argue this - if you truly believe it's more obvious that `[::-1]` produces the reversed sequence than it is for `reversed()`, then you clearly think very differently to me. – Gareth Latty May 05 '12 at 11:30
  • @Lattyware Not just me. Just grep'ped `::-1` in my Python folder. matplotlib, lxml, PIL, Django, difflib, pickle, numpy, pygame and many others use this form. But of course you know this better than their authors. :-) – DrTyrsa May 05 '12 at 11:43
  • Just because it is used in some library for a different situation it doesn't mean that it is better here, even though I would rather use `[::-1]` in this situation myself. – jamylak May 05 '12 at 12:13
  • @jamylak As I understand Lattyware insists that `reversed` should be always used instead of `[::-1]`. I just wanted to show that many good professionals think otherwise. – DrTyrsa May 05 '12 at 12:16
  • 1
    @DrTyrsa `reversed()` was added for Python 2.4, many of those libraries pre-date that, and many of those libraries will have programmers who stick to old habits. – Gareth Latty May 05 '12 at 12:35
5

Slice notation is useful here:

>>> "malayalam"[::-1]
'malayalam'
>>> "hello"[::-1]
'olleh'

See Explain Python's slice notation for a good introduction.

Community
  • 1
  • 1
Jesvin Jose
  • 22,498
  • 32
  • 109
  • 202
0
str=input('Enter a String')
print('Original string is : ',str)
rev=str[::-1]
print('the reversed string is : ',rev)
if(str==rev):
    print('its palindrome')
else:
    print('its not palindrome')
-1
x=raw_input("enter the string")
while True:
    if x[0: ]==x[::-1]:
        print 'string is palindrome'
        break
    else:
        print 'string is not palindrome'
        break
The SE I loved is dead
  • 1,517
  • 4
  • 23
  • 27