Which of the following for loops would output the below number pattern?
11111
22222
33333
44444
55555
A . for i in range(0, 5):
print(str(i) * 5)
B. for i in range(1, 6):
print(str(i) * 5)
C. for i in range(1, 6):
print(i, i, i, i, i)
D. for i in range(1, 5):
print(str(i) * 5)
Answer: B
Explanation:
Topics: for range() str() multiply operator string concatenation
Try it yourself:
for i in range(1, 6):
print(str(i) * 5)
"""
11111
22222
33333
44444
55555
"""
print(‘———-‘)
for i in range(0, 5):
print(str(i) * 5)
"""
00000
11111
22222
33333
44444
"""
print(‘———-‘)
for i in range(1, 6):
print(i, i, i, i, i)
"""
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
"""
print(‘———-‘)
for i in range(1, 5):
print(str(i) * 5)
"""
11111
22222
33333
44444
"""
You need range (1, 6)
because the start value 1 is inclusive and the end value 6 is exclusive. To get the same numbers next to each other (without a space between them) you need to make a string and then use the multiply operator string concatenation
The standard separator of the print() function is one space. print(i, i, i, i, i) gives you one space between each number. It would work with print(i, i, i, i, i, sep=”) but that answer is not offered here.
Latest PCEP-30-02 Dumps Valid Version with 124 Q&As
Latest And Valid Q&A | Instant Download | Once Fail, Full Refund