Prerequisite: Loops and String
Note: Output of all these programs is tested on Python3
Python3
Python3
Python3
Python3
Python3
1. What is the output of the following?
my_string = "geeksforgeeks"
i = "i"
while i in my_string:
print(i, end =" ")
- None
- geeksforgeeks
- i i i i i i …
- g e e k s f o r g e e k s
1. NoneExplanation: 'i' is not present in string 'geeksforgeeks'
2. What is the output of the following?
i = 0
while i < 3:
print(i)
i += 1
else:
print(0)
- 0 1 2 3 0
- 0 1 2 0
- 0 1 2
- Error
2. 0 1 2 0Explanation: The else part is executed when the condition in the while statement is false.
3. What is the output of the following?
my_string = 'geeksforgeeks'
for i in range(my_string):
print(i)
- 0 1 2 3 ... 12
- geeksforgeeks
- None
- Error
4. ErrorExplanation: range(str) is not allowed.
4. What is the output of the following?
my_string = 'geeksforgeeks'
for i in range(len(my_string)):
my_string[i].upper()
print (my_string)
- GEEKSFORGEEKS
- geeksforgeeks
- Error
- None
2. geeksforgeeksExplanation: Changes do not happen in-place, rather it will return a new instance of the string.
5. What is the output of the following?
my_string = 'geeksforgeeks'
for i in range(len(my_string)):
print (my_string)
my_string = 'a'
- gaaaaaaaaaaaa
- geeksforgeeks a a a a a a a a a a a a
- Error
- None
2. geeksforgeeks a a a a a a a a a a a aExplanation: String is modified only after ‘geeksforgeeks’ has been printed once.