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.

Python Introducing Lists Using Lists Review Split and Join

I'm not super clear on the wording here. I get the question, but I don't quite understand the answers available.

We have a list and we want to make it a string... so we split the string. so I'd do option B 'split. It's on a string'.

2 Answers

Dave StSomeWhere
Dave StSomeWhere
19,822 Points

You use join to put list entries together into a string and use split to break out a string into list entries.

Try messing with this:

string_example = 'this is a string'

print('string example is --> ', string_example)

list_from_string = string_example.split()

print('split on whitespace to get list from string --> ', list_from_string)

string_from_list = ' sep '.join(list_from_string)

print('join to get string from list --> ', string_from_list)

another_list_from_string = list(string_example)

print('every item of iterable as list entry --> ', another_list_from_string)
# output
string example is -->  this is a string
split on whitespace to get list from string -->  ['this', 'is', 'a', 'string']
join to get string from list -->  this sep is sep a sep string
every item of iterable as list entry -->  ['t', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g']

:tropical_drink: :palm_tree:

Thanks 🙏