16/18: not understanding range and concatenation problem


#1

Hello there!

I have solved exercise, but I know I don´t understand how to use the range function. My initial code was:

n = ["Michael", "Lieberman"]
# Add your function here
def join_strings(words):
  result = ""
  for item in range(len(words)):
    result += item
  return result

print join_strings(n)

… which returned the error:

Traceback (most recent call last):
  File "python", line 9, in <module>
  File "python", line 6, in join_strings
TypeError: cannot concatenate 'str' and 'int' objects

I have read through some other posts, and I know now the code should be:

n = ["Michael", "Lieberman"]
# Add your function here
def join_strings(words):
  result = ""
  for item in words:
    result += item
  return result

print join_strings(n)

My question is: is the range function only used to generate lists of integers, and not to define a range within a list of strings? Whenever we are using lists of strings, range is out of question and only += can be used?

I also tried to use:

for item in range(len(words)):
    result.append(item)

… but it returned a “append has no parameter” error (cannot remember it precisely).

There is obviously LOTS of stuff I don´t get, so any help is more than welcome.

Thanks in advance!


#2

range() produces a list:

print range(3)

so if we do:

n = ["Michael", "Lieberman"]
print range(len(n))

we get [0, 1], which so happen to be the indexes of the list, as we can see:

n = ["Michael", "Lieberman"]
for index in range(len(n)):
    print index
    print n[index]

in the example above, i already took an additional step. We used the index to get the value/item of the list using the index.

without range, we get the values of the list directly:

n = ["Michael", "Lieberman"]
for value in n:
    print value

which makes things a lot easier in this case

range is certainly useful, like when we want to update all the elements in the list:

example= [3, 5, 7]
for index in range(len(example)):
    example[index] += 2

print example

now we updated all elements in the list, increasing there value by 2


#3

Thanks a lot for the very detailed answer!

I now get the range explanation. However, I am still wondering about the append. function. The error I was getting when using

for item in range(len(words)):
    result.append(item)

was

Traceback (most recent call last):
  File "python", line 9, in <module>
  File "python", line 6, in join_strings
AttributeError: 'str' object has no attribute 'append'

Do I understand correctly that append. can only be used for integers, and not for strings?

Thanks again for all the help!


#4

no, you got that wrong i am afraid

append() is a method for a list, so it can only be used on lists, not on strings (result is a string)

just like .lower() can be used on strings, but not a list

certain methods belong to certain data types, but we need to understand classes for this, so this will come later.


#5

THANKS!

Seriously, really helpful feedback:+1::grinning:

Looking forward to learning about classes!


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.