0

I'm new to python, I'm using v2.7

#Filename:ReverseNumber.py
data=int(raw_input("Enter any number: "))
print "Reverse of the number: ",
while data!=0:
    a=data%10
    print a,
    data=data/10

So the output should come like :

Enter any number: 123
Reverse of the number: 321

but instead of this in the second line it is printing one extra space before every number.

How to overcome this?

1
  • 1
    Why not simply print str(data)[::-1] ? Commented Oct 5, 2011 at 6:31

4 Answers 4

4

You could modify your approach in a number of ways:

Create a list of your values, then join them with a blank string for printing:

out = []
while data != 0:
  out.append(data % 10)
  data /= 10
print ''.join(map(str, out))

You could skip the whole integer conversion step and just reverse the incoming string:

data = '123'
print data[-1::-1]

You could skip the integer step and join the result of reversed:

data = '123'
print ''.join(reversed(data))

You could add a backspace to your print commands for your numbers:

while data != 0:
  print '\b%d' % (data % 10)
  data /= 10

Hopefully one of those fits your requirements.

Sign up to request clarification or add additional context in comments.

2 Comments

In your first solution, map(str, out) is more concise and a bit faster.
@ChrisWong - Very true. Thanks! I've edited it to reflect this suggestion. :)
2

There is an implicit space when a is printed.

From the python docs:

"A space is written before each object is (converted and) written, unless the output system believes it is positioned at the beginning of a line. This is the case (1) when no characters have yet been written to standard output, (2) when the last character written to standard output is a whitespace character except ' ', or (3) when the last write operation on standard output was not a print statement."

So try

print "Reverse of the number:",

1 Comment

While this would eliminate the extra space before the number(s), it would not have addressed the spaces between numbers in the approach he described initially.
0

One way is to not print it until you're ready to print the whole lot. Sample code follows using strings:

data = int (raw_input ("Enter any number: "))
if data == 0:
    str = "0"
else:
    str = ""
    while data != 0:
        str = "%s%d" % (str, data % 10)
        data = data / 10
print "Reverse of the number: %s" % (str)

Comments

-2

Make a empty variable for purpose of storing reversed number, append every digit to it in a loop and then do the printing.

a = ''
while data!=0:
    a += data%10
    data=data/10
print a

3 Comments

TypeError: cannot concatenate 'str' and 'int' objects
But here I'll get "TypeError: cannot concatenate 'str' and 'int' objects"
Whoops, I haven't been in python for some time now, sorry for the TypeError. Although, the concept was fine, wasn't it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.