Captain
Captain

Reputation: 9

Reverse the given string

I'm writing a program to print the reverse of a given string. I am able to write the function to reverse the string but not able to take as many inputs as given in the test cases.

I've tried using "while" loop but I'm not able to get all the test cases as input. Maybe the syntax is wrong. I'm a newbie.

def rev(sample_string):
    return sample_string[::-1]

t_case = int(input())   #No. of testcases
i = 0

while i < t_case:
    sample_string = str(input(""))    #take as many inputs as no. of 
                                      #testcases 
    i =+ 1

print(rev(sample_string))

for sample input: 2, ab, aba -------- output should be: ba, aba //(in separate lines)

Upvotes: 0

Views: 67

Answers (1)

Frank
Frank

Reputation: 2029

If you want to save and print multiple strings, you need a datatype to do so. List would do that job:

def rev(sample_string):
    return sample_string[::-1]

t_case = int(input())   #No. of testcases
i = 0
string_list = []  # List to store the strings

while i < t_case:
    string_list.append(str(input("")))    #take as many inputs as no. of 
                                      #testcases 
    i += 1

for string in string_list:  # iterate over the list and print the reverse strings
    print(rev(string))

Upvotes: 1

Related Questions