stars = "*" * 5
stripes = "=" * 5
i = 0
while i < 5:
print(stars[:i])
print(stripes[i:])
i += 1
output:
=====
*
====
**
===
***
==
****
=
How does [:i] and [i:] work in this code to get the following output.
stars = "*" * 5
stripes = "=" * 5
i = 0
while i < 5:
print(stars[:i])
print(stripes[i:])
i += 1
output:
=====
*
====
**
===
***
==
****
=
How does [:i] and [i:] work in this code to get the following output.
[:i] means select the first element to element i-1 and [i:] means select element i to the last element
in your case, print(stars[:i]) means print stars[0] to stars[i-1] and print(stripes[i:]) means print stripes[i] to stripes["last element"] every iteration