Output of Python Programs | Set 23 (String in loops)
Prerequisite: Loops and String
Note: Output of all these programs is tested on 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
Output:
1. None
Explanation: ‘i’ is not present in string ‘geeksforgeeks’
2. What is the output of the following?
i = 0while i < 3: print(i) i += 1else: print(0) |
- 0 1 2 3 0
- 0 1 2 0
- 0 1 2
- Error
Output:
2. 0 1 2 0
Explanation: 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
Output:
4. Error
Explanation: 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
Output:
2. geeksforgeeks
Explanation: 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
Output:
2. geeksforgeeks a a a a a a a a a a a a
Explanation: String is modified only after ‘geeksforgeeks’ has been printed once.
Recommended Posts:
- Output of Python Programs | Set 22 (Loops)
- Output of C programs | Set 35 (Loops)
- Output of C programs | Set 61 (Loops)
- Output of C programs | Set 59 (Loops and Control Statements)
- Output of Java Programs | Set 43 (Conditional statements & Loops)
- Output of Python program | Set 15 (Loops)
- Output of Java Programs | Set 53 (String Comparison)
- Output of Python programs | Set 7
- Output of Python programs | Set 8
- Output of Python Programs | Set 24 (Sets)
- Output of Python Programs | Set 20 (Tuples)
- Output of Python Programs | Set 21 (Bool)
- Output of Python Programs | Set 19 (Strings)
- Output of Python Programs | (Dictionary)
- Output of Python programs | Set 9 (Dictionary)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


