Write the output of the following program:

Str = "Python Programming"

index = 0

while index < len(Str):

  if Str[index] in "aeiouAEIOU":

    print(Str[index].lower(), end=" ")

  elif index % 3 == 0:

    print(Str[index].upper(), end=" ")

  else:

    print(Str[index], end="")

    print("+", end=" ")

  index += 2

Answer:

Answer by student

The output of the program is:

-lock-

P t+ o   r+ g+ a m+ n+ 

Detailed answer by teachoo

To understand the output of the program, we need to follow these steps:

    • The program defines a string variable Str with the value "Python Programming" .
    • The program also defines an integer variable index with the initial value 0 .
    • The program uses a while loop to iterate over the characters of Str from left to right, as long as index is less than the length of Str .
    • Inside the loop, the program checks three conditions for each character of Str at the position index :
  • If the character is a vowel (either uppercase or lowercase), it prints the character in lowercase, followed by a space.
    • If the character is not a vowel and index is divisible by 3 (i.e., index % 3 == 0 ), it prints the character in uppercase, followed by a space.
    • If the character is not a vowel and index is not divisible by 3, it prints the character as it is, followed by a plus sign ( + ) and a space.
  • After printing each character, the program increments index by 2 , skipping one character in between.
  • The loop ends when index becomes equal to or greater than the length of Str .
  • The output of the program is a sequence of characters separated by spaces and plus signs, as shown above.

So, the correct answer is P t+ o   r+ g+ a m+ n+ .

-endlock-

Remove Ads
Teachoo · Class 11 Explore Class 11
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.