GoCoding.org

Program to reverse the input phrase using Python

by | Apr 12, 2021 | Python Programs

Introduction

In section our task is to reverse the input phrase.

Program to reverse the input phrase

Program

def phrase_reverse(phrase):
# Split words
    ele = phrase.split(' ')
# Reverse string and join
    rev = ' '.join(reversed(ele))
    return rev
   
input_string = input("Enter the phrase to reverse: \n")
print(phrase_reverse(input_string))

Output

Program to reverse the input phrase Output

Explanation

To achieve our task we have firstly split each word from the input phrase using split() function and then joined the reversed words using spaces. The reversed phrase is stored in variable rev and printed on the screen.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.