Informatics Practices
Find the output of the following program segment:
country = 'INDIA'
for i in country:
print (i)
Answer
I
N
D
I
A
Working
Here's the detailed explanation:
1. String Assignment:
country = 'INDIA'
This line assigns the string 'INDIA' to the variable country.
2. for i in country:: The for loop iterates over each character in the string country. The variable i will take on the value of each character in the string one at a time.
3. print(i): This prints the current character stored in the variable i for each iteration.
Let’s analyze the loop iteration by iteration:
- In the first iteration,
iis'I', soprint(i)outputs:I. - In the second iteration,
iis'N', soprint(i)outputs:N. - In the third iteration,
iis'D', soprint(i)outputs:D. - In the fourth iteration,
iis'I', soprint(i)outputs:I. - In the fifth iteration,
iis'A', soprint(i)outputs:A.
Putting it all together, the output will be each character of the string 'INDIA' printed on a new line:
I
N
D
I
A
Related Questions
What is the difference between else and elif constructs of if statement?
Find the output of the following program segment:
for i in range(20, 30, 2): print(i)Find the output of the following program segment:
i = 0; sum = 0 while i < 9: if i % 4 == 0: sum = sum + i i = i + 2 print (sum)Write the output of the following:
for i in '123' : print ("Message",i,)