Positional Argument Unpacking Exercise - String concatenation issue

Link - https://www.codecademy.com/courses/learn-python-3/lessons/learn-python-function-arguments/exercises/positional-argument-unpacking

I wrote the following code to substitute for .join()

from os.path import join

path_segment_1 = "/Home/User"
path_segment_2 = "Codecademy/videos"
path_segment_3 = "cat_videos/surprised_cat.mp4"

# join all three of the paths here!
#print(join(path_segment_1, path_segment_2, path_segment_3))

def myjoin(*args,joined_string=None):
  if joined_string is None:
    joined_string = []
  print(args[0])
  for arg in args:
    joined_string += arg
    print(joined_string)
    print("\n")
  return joined_string

myjoin(path_segment_1, path_segment_2, path_segment_3)

And my output looks like this:

/Home/User
[’/’, β€˜H’, β€˜o’, β€˜m’, β€˜e’, β€˜/’, β€˜U’, β€˜s’, β€˜e’, β€˜r’]

[’/’, β€˜H’, β€˜o’, β€˜m’, β€˜e’, β€˜/’, β€˜U’, β€˜s’, β€˜e’, β€˜r’, β€˜C’, β€˜o’, β€˜d’, β€˜e’, β€˜c’, β€˜a’, β€˜d’, β€˜e’, β€˜m’, β€˜y’, β€˜/’, β€˜v’, β€˜i’, β€˜d’, β€˜e’, β€˜o’, β€˜s’]

[’/’, β€˜H’, β€˜o’, β€˜m’, β€˜e’, β€˜/’, β€˜U’, β€˜s’, β€˜e’, β€˜r’, β€˜C’, β€˜o’, β€˜d’, β€˜e’, β€˜c’, β€˜a’, β€˜d’, β€˜e’, β€˜m’, β€˜y’, β€˜/’, β€˜v’, β€˜i’, β€˜d’, β€˜e’, β€˜o’, β€˜s’, β€˜c’, β€˜a’, β€˜t’, β€˜β€™, β€˜v’, β€˜i’, β€˜d’, β€˜e’, β€˜o’, β€˜s’, β€˜/’, β€˜s’, β€˜u’, β€˜r’, β€˜p’, β€˜r’, β€˜i’, β€˜s’, β€˜e’, β€˜d’, '’, β€˜c’, β€˜a’, β€˜t’, β€˜.’, β€˜m’, β€˜p’, β€˜4’]

Can someone please explain me why when I print args[0] before the iteration I get β€˜/Home/User’ as a whole but when I print it again in the for loop the whole string is exploded into characters?

Below is the actual solution. I do not understand the necessity to assign joined_string = args[0] for the iteration to work.

def myjoin(*args):
  joined_string = args[0]
  for arg in args[1:]:
    joined_string += '/' + arg
  return joined_string

Any help is appreciated, Thanks!

The index args[0] is the first argument which in this case is path_segment1 and the print just acts on that string.

For the actual join you’re using a list (is this interntional?). With the += operator the list adds the contents of an iterable to the list element by element and strings treated as iterables pass each character as an element.

You could indeed do it without that assignment but I don’t believe solutions were ever intended as a perfect answer. There are often several ways to complete the same task.

Might be worth considering the use of the string ``.join method for this task (unless it hasn’t been introduced yet).

This is the equivalent of list.extend() which is treated as a sequence being concatenated to a sequence. A string is treated as a sequence.

The exercise here was to create a β€˜join’ function manually. I see how silly my question is now, guess I had a brain freeze! I was not thinking in terms of string/list and got too hung up on the solution. Instead of assigning arg[0] as shown in solution, I just declared it as an empty string and it worked fine. Thanks for the clarification!

Yes I was not focusing on the list/string behavior. Thanks for the help!

1 Like