19 AnswersNew Answer
#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.....
Rahul Hemdev you are very welcome😊 You'll do great!💪 Happy SoloLearning 🍻🍻
Rahul Hemdev sorry buddy, I misunderstood your question?😬 I read it quickly and went the other way.😝 I will delete my answer👍
* 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, t)) 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.
HonFu thanks a lot buddy👍🍻🍻
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.👍
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'
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..
Applying * will unpack the iterable to each individual element.. s String is also iterable so it's works like extracts charecters of the string....
s = 'hello' print(s) -> output is: 'hello' print(*s) -> output is: 'h e l l o'
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.....
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
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
Danijel Ivanović :)
This pleascode i want
Mohammed Niems The code... https://code.sololearn.com/c169MH6x8yEm/?ref=app
In html, When i use / for ending code it displays the same results as without / why?plz answer me also
cause you put * before the s Example: hey = "I am an example" print(*hey)