## Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

### Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

###### Doug Terra

315 Points# Task 2 of 3: Function to return string

My Summarize function does return a string when run in my external python editor, but not in the grader, I must be close...

def add_list (thelist): total = 0 for ivar in thelist: total = total + ivar return total add_list([1, 2, 3]) def summarize (alist): total = 0 for ivar in alist: total = total + ivar return 'The sum of', alist,'is',total,'.' summarize([1, 2, 3])

```
# add_list([1, 2, 3]) should return 6
# summarize([1, 2, 3]) should return "The sum of [1, 2, 3] is 6."
# Note: both functions will only take *one* argument each.
def add_list (thelist):
total = 0
for ivar in thelist:
total = total + ivar
return total
add_list([1, 2, 3])
def summarize (alist):
total = 0
for ivar in alist:
total = total + ivar
return 'The sum of', alist,'is',total,','
summarize([1, 2, 3])
```

## 1 Answer

###### Gunhoo Yoon

5,027 PointsUpdate:

You can still read what I've wrote down there but this is probably the solution that you are best familiar with.

```
def summarize (alist):
total = 0
for ivar in alist:
total = total + ivar
return 'The sum of {} is {}.'.format(alist, total)
```

---------------------------Older-----------------------------------

```
return 'The sum of', alist,'is',total,','
#You get
#('The sum of', [1, 2, 3] 'is', 6 '.') <- this is not a String.
```

I don' know how far you've learned but use this gimmick which you will learn anyways.

```
return "The sum of %s is %s." % (alist, total)
#or
return 'The sum of', str(alist), 'is', total, ','
```

First one create template string with placeholders which you can dynamically change. %s is a placeholder and % after String is operator that will try to connect values to your placeholder.

Second one explicitly tell we want string version of our list. This isn't much recommended though.

It is really weird question because when I tried the second method it passed which it shouldn't.

What you get from returning things like `return 'hello', 'my', 'world'`

is ('hello', 'my', 'world') which is called tuple. This is not a string but somehow the grader accepts it.

Try running this code at the end of your code with Python interpreter.

```
print(type(summarize([1, 2, 3])) is str) #false
print(type(summarize([1, 2, 3])) is tuple) #true
```

This checks whether your result from summarize() is string or not.