0
why is the output of s='hello' > print (*s) > h e l l o and not hello ?
3 ответов
+ 4
because the star (*) unpacks an iterable. And all a string is is an array (or list) of characters
you basically did this:
s = ['h', 'e', 'l', 'l', 'o']
print(*s)
# output
h e l l o
+ 2
The print function prints every argument you give it separated by a space.
Example:
print('Hello', 'World')
Both strings will be separated by a space.
*, as Slick said, 'unpacks' s and passes all the letters separately as args.
You can change the default space with the parameter sep:
print('Hel', 'lo', sep='*')
Now instead of a space, * is used as the separator, and the output will be 'Hel*lo'.
And this, of course, also works when you unpack an iterable and pass it to print.
+ 1
Haha, HonFu I honestly only knew that cause you dropped that knowledge a while ago. And I've seen the "sep" param, but never used it, thanks for the lesson again man!