-1

Here is the image,

enter image description here

Here is what i tried playing with join function but i don't get what's going on here if anyone out there encountered with same then please help me out!.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555

3 Answers3

2

Strings can be iterated through, and when you do iterate through one, it will spin through each individual letter.

str.join(iterable) will join the iterable inside the brackets together using the string outside the brackets.

If the iterable was a list, it'd be easy to see what is going on

>>> l = ["Alice", "Ben", "Casey"]
>>> ", ".join(l)
"Alice, Ben, Casey"

And with a string,

>>> "-".join("Hello!")
>>> # Same as "-".join(["H", "e", "l", "l", "o", "!"])
"H-e-l-l-o-!"
Sam Rockett
  • 3,145
  • 2
  • 19
  • 33
0

String is a sequence of characters. Join function concatenates the sequence (iterable) to a new string by joining them on the string you called the function on. In this case it's s with value of 'HI'.

Its not different than:

print(" ".join("hello", "world"))

That results in "hello world".

Aleksei Maide
  • 1,845
  • 1
  • 21
  • 23
0

The thing is, string.join receives a iterable as a parameter, so the string you are passing it, will be treated as a iterable and not a string, so you will do b, joined with y joined with e. If you do s.join(['bye']), then the output will be the one I assume you were expecting

jtagle
  • 302
  • 1
  • 8