KnowledgeBoat Logo
|

Informatics Practices

Find the output of the following program segment:

country = 'INDIA'
for i in country:
   print (i)

Python Control Flow

2 Likes

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, i is 'I', so print(i) outputs: I.
  • In the second iteration, i is 'N', so print(i) outputs: N.
  • In the third iteration, i is 'D', so print(i) outputs: D.
  • In the fourth iteration, i is 'I', so print(i) outputs: I.
  • In the fifth iteration, i is 'A', so print(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

Answered By

1 Like


Related Questions