I am writing a python program which no. of string user wants to enter as integer and then takes N number of strings from user and then separates strings based on their index and print even strings and odd string separated by space.
For Example: Inputs from user: 1
and output: 2
and had written this code in pycharm IDE:
tc=input("Enter the no. of test cases you want: ")
for j in range((int(tc))):
n=list(map(int,input("Enter a string: ").split()))
continue
print(1)
for i in range(len(n)):
if i%2==0:
print(n[i],end="")
print(end=" ")
for i in range(len(n)):
if i%2!=0:
print(n[i],end="")
but the problem I am facing is that when I input first string(hacker) and then it will ask for second input and when I input second string(sand) and press enter then it gives output of second string only(sn ad), however I want output as shown in the image above.
-
Please update your question with the text of the sample input and required output.quamrana– quamrana2020年09月14日 13:06:20 +00:00Commented Sep 14, 2020 at 13:06
2 Answers 2
You need to collect the inputs and then apply the same transformation to each one:
tc = input("Enter the no. of test cases you want: ")
s = []
for j in range((int(tc))):
s.append(input("Enter a string: "))
for n in s:
for i in range(len(n)):
if i % 2 == 0:
print(n[i], end="")
print(end=" ")
for i in range(len(n)):
if i % 2 != 0:
print(n[i], end="")
print()
Output:
hce akr
sn ad
Not quite as you specify.
Comments
A simple answer for your question
n = int(input())
strings = [input() for _ in range(n)]
result = []
for string in strings:
string_odd = string[::2]
string_even = string[1::2]
string = string_odd+' '+string_even
result.append(string)
for res in result:
print(res)
Output
hce akr
sn ad