+ 5
[Solved] Why is the output of the following code 'H e l l o'?
s = "Hello" print(*s)
21 Respuestas
+ 6
#hoping this code may help you..Rahul Hemdev
# *s is similar to " ".join(s).
s = tuple(range(10))
print(*s)
#output : 0 1 2 3 4 5 6 7 8 9
s = list(range(10))
print(*s)
#output : 0 1 2 3 4 5 6 7 8 9
s = "string"
print(*s)
#output : s t r i n g
print(" ".join(s)) #output : s t r i n g
S = ["AB","CD","EFG"]
print(S) #output : ["AB","CD","EFG"]
print(*S) #output : AB CD EFG
S = tuple(S)
print(S) #output : ("AB","CD","EFG")
print(*S) #output : AB CD EFG
Edit : am also little know about python.....
+ 11
Rahul Hemdev you are very welcome😊
You'll do great!💪
Happy SoloLearning 🍻🍻
+ 9
Rahul Hemdev sorry buddy,
I misunderstood your question?😬
I read it quickly and went the other way.😝
I will delete my answer👍
+ 9
* basically 'unpacks' any iterable into what's in it.
A string becomes letters, a list of ints becomes ints etc.
This can be used to call a function that takes several arguments.
def add(x, y):
#dumb example
return x+y
So you call this by handing in two arguments.
print(add(5, 7))
Now if you have an iterable of length 2, let's say a tuple, you can call the function with it by indexing.
t = 5, 12
print(add(t[0], t[1]))
But that looks kind of messy.
Here our star comes in.
You write...
print(add(*t))
... and now the tuple is unpacked and its contents directly used as args for the functions.
(There are other use cases.)
Now print is a function. It can take as many args as you like, and they will be printed with one space separated.
print(5, 'Hello', 7)
print is a function like 'add' before, only that you can add args as many as you want.
So if you wrote...
print(*t)
... the tuple t is unpacked and its contents given to the function print, which is doing its thing with them.
+ 9
HonFu thanks a lot buddy👍🍻🍻
+ 8
Rahul Hemdev
You can unpack an iterable without knowing the exact number of items in it. For that, you provide a variable that may collect a list of values. This is done by placing an asterisk before the name.
Note:
When using the *variable syntax, the 'variable' will always be a list, even if the original type wasn't a list.
Example of unpacking a string:
start, *s = "Hello"
print(start)
# Output: 'H'
print(s)
# Output: ['e', 'l', 'l', 'o']
I'm just learning Python a bit..😁
For more knowledge we need to ask a real Pythonist💪, I hope he doesn't mind me disturbing him but HonFu🍻🍻 can give us the right answer.👍
+ 7
Rahul Hemdev
Always welcome buddy!😊
Thank you for understanding.👍🍻
I can say that for sequences such as string, list and tuple, * is a repetition operator. I will research..
+ 5
Applying * will unpack the iterable to each individual element.. s string is also iterable so it's works like extracts charecters of the string....
+ 5
Danijel Ivanović A big big thanks for all your hard work!!! I've understood the topic now.
Jayakrishna🇮🇳 and HonFu Thanks guys... Your examples have made the topic quite clear.
@Everyone I really appreciate the SoloLearn community, as everyone is always so helpful. I've just started my Python journey, and I hope to be good at Python just like you guys.....
+ 4
s = 'hello'
print(s) -> output is: 'hello'
print(*s) -> output is: 'h e l l o'
+ 4
Jayakrishna🇮🇳 Sorry, but it's still unclear to me...
Lothar Yes, but what I meant to ask was, WHY does the output change due to the addition of '*'?
Danijel Ivanović Yes, I've already gone through the lesson, but it's nowhere mentioned what does print(*s) do... It's only about multiplying strings with strings
+ 4
Danijel Ivanović No problem man... It happens. You took efforts to put all those references... So thank you!!!
What's the correct answer by the way? ;-P
+ 3
+ 3
" ",jion(s)
+ 3
Ayoife Ayinde-Oladeinde Man if you don't know an answer, please don't post anything, instead refer other's explanation.
+ 2
0
In html, When i use / for ending code it displays the same results as without / why?plz answer me also
0
I don't know why
- 2
cause you put * before the s
Example:
hey = "I am an example"
print(*hey)