How does the join( ) method work?

Question

How does the join( ) method work?

Answer

.join() is another method available to strings. It works by attaching it to a string, like ” “ (a space), and then telling it what to “join” with that string between each element.
So when we use it with a list, it separates each item in the list by the string you attached it to and creates a single string result. Take a look at the example below:

my_greeting = ["Hello", "coder", "people", "!"]  # Note: there are no spaces
print " ".join(my_greeting)  # Prints "Hello coder people !"
print "--".join(my_greeting)  # Prints "Hello--coder--people--!"
8 Likes

2 posts were split to a new topic: Why is my Code Being Rejected?

This function gave me a hard time yesterday!
I wish it was something like this instead: join(" ", list). Yes, I know I could probably create a custom function for that, but that’s not the point when I sitll have to learn the syntax of this function which is kinda unusual one for me.

4 Likes

It is unusual. I’d heard Python was a simple language, but i haven’t found it so. A lot of ( instead of [ or {…

measuring how difficult a programming language is by different type of brackets is horrible unit of measurement.

1 Like

Just an observation. I haven’t taken a programming language in many years, but I remember many of them being more intuitive than Python.

Looping in Python (the FOR loop) is also a bit, uh, different…

Definitely an interesting language though!

Chuck

On Tue 1/12/20 8:31 AM , Stetim94 via Codecademy Forums [email protected] sent:

board=[]
for i in range(0,5):
  board.append(["O"]*5)

def print_board(board_in):
  for x in board_in:
   print "".join(x)
print print_board(board)

It is printed without the " but it says it’s wrong. It also prints none below the board. Why?

What is the error? Or exercise url?

I think you might need spaces in each row so you get:

O O O O O

while you now have:

OOOOO

print_board does not return anything, you attempt to print the returned result, giving None

1 Like

That would make it a built-in rather than a str method. The inner workings would be more complex, thus less optimized.

By writing .join() as a method of the str class, the interpreter does very little of the validation work. If we try to call the method on anything other than a str it will raise an AttributeError telling us that the object does not have a ‘join’ attribute. If we try to pass any object that is not iterable it will raise a TypeError since the method can only join an iterable.

The above describes a separation of concerns, one handled by the interpreter, the other by the method’s own error trapping.

Once you gain familiarity with the many built in classes and their associated methods, you’ll find the language very intuitive. Think of it as leaning how to use an HP calculator after having spent years working with a TI. Reverse Polish Notation takes some getting used to, but once we do, we love it. (Mind, millennials might not be familiar with either.)