[Solved] Why is the output of the following code 'H e l l o'? | SoloLearn: Learn to code for FREE!

+3

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

s = "Hello" print(*s)

9/29/2020 1:35:48 PM

Rahul Hemdev

+4

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

+9

Rahul Hemdev you are very welcome😊 You'll do great!💪 Happy SoloLearning 🍻🍻

+8

Rahul Hemdev sorry buddy, I misunderstood your question?😬 I read it quickly and went the other way.😝 I will delete my answer👍

+8

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

+8

HonFu thanks a lot buddy👍🍻🍻

+7

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

This article is NOT meant to be an introduction to programming. ... print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False) # Print objects to ... The defaults are: m =0 , n=-1 , step=1 . s[1:3] ⇒ ' el'

+6

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

+4

Applying * will unpack the iterable to each individual element.. s String is also iterable so it's works like extracts charecters of the string....

+4

s = 'hello' print(s) -> output is: 'hello' print(*s) -> output is: 'h e l l o'

+4

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

+3

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

+3

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

+2

Danijel Ivanović :)

+1

This pleascode i want

+1

Mohammed Niems The code... https://code.sololearn.com/c169MH6x8yEm/?ref=app

+1

" ",jion(s)

0

In html, When i use / for ending code it displays the same results as without / why?plz answer me also

-2

cause you put * before the s Example: hey = "I am an example" print(*hey)