Sunday, 23 January 2022

Write a Python script using class to reverse a string word by word

Slip 9 Q 1

CODE :

METHOD 1

def reverse_words(s):
    return ' '.join(reversed(s.split()))

str1=input('Enter a string : ')
print(reverse_words(str1))

OUTPUT:




METHOD 2

def listtostr(strlist):
    newstr=""
    for X in strlist:
        newstr += X+' '
    return newstr

str1=input("Enter a string to reverse : ")
strlist=str1.split(" ")
strlist.reverse()
print(listtostr(strlist))

OUTPUT :



No comments:

Post a Comment

Write a Java program to display given extension files from a specific directory on server machine.

 DOWNLOAD     SLIP14Q2 /** * STEPS TO RUN CODE * Step 01 compile the code * Step 02 run the code * Step 03 give a file directory locatio...