Write the output of the following program:

Str = "Computer Science"

index = 0

while index < len(Str):

  if Str[index] in "aeiouAEIOU":

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

  elif index % 2 == 0:

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

  else:

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

    print("#", end=" ")

  index += 1

Answer:

Answer by student

The output of the program is:

 

c O m p# U t# E r#   S# c I E n# c E

Detailed answer by teachoo

-lock-

  • The program uses a while loop to iterate over the characters of the string Str , which is "Computer Science" .
  • The loop starts with index = 0 , which is the first position of the string, and ends when index becomes equal to or greater than len(Str) , which is the length of the string, or 15 in this case.
  • In each iteration of the loop, the program checks three conditions for the character at the current index:
    • If the character is a vowel (either lowercase or uppercase), it prints the character in uppercase, followed by a space. For example, if the character is "o" , it prints "O " .
    • If the character is not a vowel and the index is even , it prints the character in lowercase, followed by a space. For example, if the character is "C" and the index is 0, it prints "c " .
    • If the character is not a vowel and the index is odd , it prints the character as it is, followed by a "#" and a space. For example, if the character is "m" and the index is 1, it prints "m# " .
  • After printing the character according to the condition, the program increments index by 1 using index += 1 , which means index = index + 1 . This moves to the next position of the string.
  • The loop repeats until all the characters of the string are processed.

The output of the program is a combination of all the characters printed in each iteration of the loop, as shown below:

c O m p# U t# E r#   S# c I E n# c E

-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.