Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

* in print function in python

text = 'PYTHON'
for index in range(len(text)):
    print(*text[:index + 1])

The * in the print function is producing a space between the characters on sys.stdout. Can someone please tell me what is it called and what does it actually do?

like image 794
Niraj Raut Avatar asked Oct 28 '25 10:10

Niraj Raut


1 Answers

The print of * for a text is equal as printing print(text[0], text[1], ..., text[n]) and this is printing each part with a space between. you can do

text = 'PYTHON'
for index in range(len(text))
    print("".join(list(text)[:index + 1]))

or

text = 'PYTHON'
for index in range(len(text))
    print(*text[:index + 1], sep='')

that will print each part without space in between. Output

P
PY
PYT
PYTH
PYTHO
PYTHON
like image 184
Leo Arad Avatar answered Oct 30 '25 00:10

Leo Arad