What is the output of the following code.l = [‘Logik', ‘works'] for i in l: i.swapcase() print(l)
Question
What is the output of the following code.l = [‘Logik', ‘works'] for i in l: i.swapcase() print(l)
Solution
The output of the code will be ['Logik', 'works'].
This is because the swapcase() function is not modifying the original list. It creates a new string with swapped cases, but this new string is not being stored or used to update the original list.
Here's the step by step explanation:
- The list
lis defined with two strings: 'Logik' and 'works'. - The
forloop iterates over each string in the list. - The
swapcase()function is called on each string. This function returns a new string where all the uppercase letters are converted to lowercase, and vice versa. However, this new string is not stored or used to update the original list. - After the loop finishes, the
print(l)statement prints the original list, which has not been modified.
If you want to modify the original list, you need to update the list with the new strings. Here's how you can do it:
l = ['Logik', 'works']
for i in range(len(l)):
l[i] = l[i].swapcase()
print(l)
This code will output: ['lOGIK', 'WORKS']
Similar Questions
What will be the output of the following Python code?x = ['xy', 'za']for i in x: i.swapcase()print(x)
Choose the Correct Answer(s)What will be the output after the following statements?x = 'Python Jobs' print(x.swapcase())OptionspYTHON jOBSPYTHON JOBSPython Jobspython jobs
What is the output of the following code in Python?
What does this script print?l1 = [1, 2, 3]l2 = l1l1.append(4)print(l2)
Write the output of the following code :L=list(https://logikworks.in/)print(L[20 : -1])
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.