To print text using nested loops in python, we can use two for loops.
Example:
main.py120 chars5 lines
This will print "Hello World" 12 times as the outer loop will run 3 times, and the inner loop will run 4 times for each iteration of the outer loop.
We can further customize the output by using string formatting to insert values into the string, and the end
parameter of the print()
function to control the line breaks.
Example:
main.py155 chars6 lines
This will print:
main.py200 chars6 lines
Here we are using the f
format specifier to insert the values of i
and j
into the string. We are also using \t
to insert a tab character between the values, and \n
to insert a newline character at the end of each inner loop.
gistlibby LogSnag