+ 5

[Solved] Why is the output of the following code 'H e l l o'?

s = "Hello" print(*s)

29th Sep 2020, 1:35 PM
Rahul Hemdev
Rahul Hemdev - avatar
21 Answers
+ 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.....
29th Sep 2020, 8:22 PM
Jayakrishna 🇮🇳
+ 11
Rahul Hemdev you are very welcome😊 You'll do great!💪 Happy SoloLearning 🍻🍻
30th Sep 2020, 5:57 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 9
Rahul Hemdev sorry buddy, I misunderstood your question?😬 I read it quickly and went the other way.😝 I will delete my answer👍
29th Sep 2020, 4:26 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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.
29th Sep 2020, 8:29 PM
HonFu
HonFu - avatar
+ 9
HonFu thanks a lot buddy👍🍻🍻
29th Sep 2020, 9:24 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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.👍
29th Sep 2020, 8:06 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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..
29th Sep 2020, 7:10 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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....
29th Sep 2020, 1:50 PM
Jayakrishna 🇮🇳
+ 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.....
30th Sep 2020, 3:11 AM
Rahul Hemdev
Rahul Hemdev - avatar
+ 4
s = 'hello' print(s) -> output is: 'hello' print(*s) -> output is: 'h e l l o'
29th Sep 2020, 2:40 PM
Lothar
Lothar - avatar
+ 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
29th Sep 2020, 3:26 PM
Rahul Hemdev
Rahul Hemdev - avatar
+ 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
29th Sep 2020, 5:30 PM
Rahul Hemdev
Rahul Hemdev - avatar
30th Sep 2020, 7:58 AM
Rahul Hemdev
Rahul Hemdev - avatar
+ 3
" ",jion(s)
23rd Oct 2020, 3:25 AM
Nannapaneni Saikiran
Nannapaneni Saikiran - avatar
+ 3
Ayoife Ayinde-Oladeinde Man if you don't know an answer, please don't post anything, instead refer other's explanation.
5th Nov 2020, 2:14 PM
Rahul Hemdev
Rahul Hemdev - avatar
1st Oct 2020, 3:14 AM
Rahul Hemdev
Rahul Hemdev - avatar
0
In html, When i use / for ending code it displays the same results as without / why?plz answer me also
1st Oct 2020, 9:18 AM
shubham Bhati
0
I don't know why
5th Nov 2020, 1:42 PM
Ayoife Ayinde-Oladeinde
Ayoife Ayinde-Oladeinde - avatar
- 2
cause you put * before the s Example: hey = "I am an example" print(*hey)
30th Sep 2020, 3:27 PM
Cleen
Cleen - avatar