Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to reverse a number in python

num = 123456
print(str(num)[::-1])
Comment

reverse a number in python

# Python string/number reverser
example_number = 12143
example_string = "Hello there"

def reverse(thing): 
    thing = str(thing) # convert it to a string, just in case it was a number
    list_of_chars = [char for char in thing]
    reversed_list_of_chars = []
    x = -1
    
    for char in list_of_chars:
      reversed_list_of_chars.append(list_of_chars[x])
      x += -1
    
    reversed_thing = ''.join(reversed_list_of_chars)
    
    return reversed_thing
    # Or alternatively do:
    print(reversed_thing)

# Run it by doing this
reverse(example_number)
reverse(example_string)
Comment

PREVIOUS NEXT
Code Example
Python :: what is cpython 
Python :: convert math equation from string to int 
Python :: how to decode recv data in python 
Python :: no of words in a string in python 
Python :: graphics.py how to make a button 
Python :: read excel by row and output to txt 
Python :: numpy distance of consecutive elements 
Python :: python typewriter effect 
Python :: how to uninstall python 
Python :: binary tree deletion 
Python :: noob python 
Python :: RMSE value from cross validation 
Python :: webex teams api attach file 
Python :: flatten list in python 
Python :: append numeric number in and auto increment in using pandas 
Python :: count number of subdirectories 
Python :: filter function in python 
Python :: gui def python 
Python :: tkinter window minsize 
Python :: Python Print Variable Using the f-string in the print statement 
Python :: bad request 400 heroku app 
Python :: Using replace() method to remove newlines from a string 
Python :: what is in the python built in namespace 
Python :: stackoverflow python 
Python :: python remove multiple element from list by index 
Python :: python create pem file 
Python :: Passing Arrays to Methods 
Python :: what is tkinter in python 
Python :: class chain methods python 
Python :: getting python class from string 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =