-2
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.

  • One of the first chapters in the tutorial deals with [strings](https://docs.python.org/3/tutorial/introduction.html#strings) and this is explained there. – Matthias Dec 21 '22 at 07:40

1 Answers1

0

[: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

ryan chandra
  • 321
  • 3
  • 11