Q:

Print the reverse of a string that contains digits in Python

belongs to collection: Python String Programs

0

A function is the collection of code that creates to perform a specific task and work for various inputs. When we have to do the same work after an interval then the function reduces the length of code, time complexity, etc. Here, we will write a function in Python that takes as input a positive integer which will be given by the user and returns the integer obtained by reversing the digits. Before going to solve this problem, we will learn a little bit about the function and how to create it in the Python programming language.

Syntax to create a function in Python:

    # definition
    def function_name(parameters):
	    ''' function statement that specifies the work 
	    of the function i.e body of function.'''
	    return(expression)

    # function calling
    print(functionname(parameters)

The function block begins with the "def" keyword and "function_name". In the parenthesis, it may have an argument or not.

Now, let's start to create a function in Python that returns the integer obtained by reversing the digits. Before going to solve the above problem, assume the name of a function is the reverse(n) and the parameter n which value will be provided by the user. The function reverse(n) returns the integer obtained by reversing the digits in n.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

# function definition that will return 
# reverse string/digits 
def reverse(n):
    # to convert the integer value into string
    s=str(n) 
    p=s[::-1]
    return p 

# now, input an integer number
num = int(input('Enter a positive value: '))

# Calling the function and printing the result 
print('The reverse integer:',reverse(num))

Output

RUN 1:
Enter a positive value: 123456
The reverse integer: 654321

RUN 2:
Enter a positive value: 362435
The reverse integer: 534263

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Convert a String to camelCase in Python... >>
<< Find the ASCII value of each character of the stri...