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
2 Likes
Related Questions
What will be the output of the following code segment?
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] del myList[:5] print(myList)What will be the output of the following code segment?
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] del myList[::2] print(myList)Differentiate between append() and insert() methods of list.
Write a program to read a list of n integers (positive as well as negative). Create two new lists, one having all positive numbers and the other having all negative numbers from the given list. Print all three lists.