Python Program to Reverse a String

    Strings are sequences of characters. They can be enclosed in quotation marks to make them appear as a single unit, and they can be accessed in several ways.

    Program Code

    # Get input string
    input_str = input("Enter a string: ")
    # Split the input string into a list of words
    words = input_str.split()
    # Reverse each word in the list
    words = [word[::-1] for word in words]
    # Join the list of words into a string
    output_str = " ".join(words)
    # Display the resultant string

    Input Given:

    Enter a string: moc.reppePudE

    Output Expected:

    Code Explanation

    1. The input string is split into a list of words using the split() method.
    2. The list of words is iterated over using a for loop.
    3. Each word is reversed using the [::-1] slicing technique.
    4. The list of reversed words is joined into a string using the join() method.
    5. The resultant string is displayed.
    Disclaimer: While we make every effort to update the information, products, and services on our website and related platforms/websites, inadvertent inaccuracies, typographical errors, or delays in updating the information may occur. The material provided on this site and associated web pages is for reference and general information purposes only. In case of any inconsistencies between the information provided on this site and the respective product/service document, the details mentioned in the product/service document shall prevail. Subscribers and users are advised to seek professional advice before acting on the information contained herein. It is recommended that users make an informed decision regarding any product or service after reviewing the relevant product/service document and applicable terms and conditions. If any inconsistencies are observed, please reach out to us.

    Latest Articles

    Related Stories

    Leave A Reply

    Please enter your comment!
    Please enter your name here

    Join our newsletter and stay updated!