Monday 29 August 2016

Python program to reverse a string without using slice and reversed functions

A simple python program to reverse a given string. Here we are not going to use slice operation or reversed function, which easily can reverse a string without lot of code. we are going to discuss about them in here : using slice and using reversed function.

The logic here is we read a string, read every character from the end and add it to new string.

Refer below for screenshot and program,




PROGRAM : 

input_string = str(input('Enter any string : '))


def reverse(string):
    i = len(string)
    rev = ""
    while i != 0:
        rev = rev + string[i - 1]
        i -= 1
    return rev


print("Reversed String is :" + reverse(input_string))

OUTPUT :  
Python - String reverse
Python - String reverse