Reverse an integer
Take an integer and reverse its digits. The result is also an integer. Example: 83657 becomes 75638.
#!/usr/bin/env python def reverse_int(n): return int(str(n)[::-1]) n = 83657 print n # 83657 print reverse_int(n) # 75638
Summary: convert the number to string, reverse the string, then convert it back to integer. Details:
"83657" which is a string -> reverse it, we get
int("75638") converts it to an integer.
If you want to concatenate a string and an integer, first you need to convert the integer to string. Example:
n = 83657 print "The value of n is " + n # error, won't work print "The value of n is " + str(n) # OK