निम्नलिखित का आउटपुट क्या है?
What is the output of the following ?
x = 'abcd'
for i in range(x):
print(i)
A)
B)
C)
D)
Explanation:
The answer is (C) error.
Reason:
The code for i in range(x):
will throw an error because x
is a string ('abcd'
), and range()
expects an integer. You can't pass a string to range()
.
Fix:
You need to use len(x)
to get the length of the string:
for i in range(len(x)):
print(i)
This will print the indices (0, 1, 2, 3). But as the code is written, it will result in an error.