Informatics Practices

Write the output of the following Python program code:

Str2 = list("Cam@123*")
for i in range(len(Str2)-1):
   if i==5:
      Str2[i] = Str2[i]*2
   elif (Str2 [i].isupper()):
      Str2 [i] = Str2 [i]*2
   elif (Str2 [i].isdigit()):
      Str2 [i] = 'D'
print(Str2)

Python List Manipulation

3 Likes

Answer

['CC', 'a', 'm', '@', 'D', '22', 'D', '*']

Working

In the given code, Str2 is initialized as a list of characters from the string "Cam@123*", resulting in ['C', 'a', 'm', '@', '1', '2', '3', '*']. The for loop iterates over the indices of Str2 from 0 to len(Str2) - 1. Within the loop, three conditions modify the elements of Str2: if the index i is 5, the character at Str2[i] is multiplied by 2, if the character is uppercase, it is also multiplied by 2, if the character is a digit, it is replaced with 'D'. After the loop, Str2 becomes ['CC', 'a', 'm', '@', 'D', '22', 'D', '*'], which is printed as the final output.

Answered By

3 Likes


Related Questions