Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (Retired) Shopping List Lists and Strings

split list on the spaces

how to split Hi, i'm Treehouse on spaces?

greeting_list.py
full_name='Tea Kokot Klancnik'
name_list=['Tea', 'Kokot', 'Klancnik']

4 Answers

You are looking for the .split() method:

full_name='Tea Kokot Klancnik'
name_list= full_name.split(' ')
Anish Walawalkar
Anish Walawalkar
8,534 Points

you can use the .split() method. you can specify what you want to split the string on. In your chase you want to split on spaces:

full_name = 'Tea Kokot Klancnik'
name_list = full_name.split(' ')

say you wanted to to split on commas (,) then

full_name = 'John, Doe, April, Adams'
name_list = full_name.split(',')
Nitin George Cherian
Nitin George Cherian
4,410 Points

If you want to split on spaces, just use

full_name.split() # No arguments for split method

If no argument are specified, it splits by space by default

Sai Kiran Dasika
PLUS
Sai Kiran Dasika
Courses Plus Student 7,278 Points

If you want to split on spaces you can just use this code.

a_string = "this is a string"

a_string = a_string.split(' ')

This will produce a result of

["this", "is", "a", "string"]