Split strings
You've learned how to do some useful operations with strings, like concatenating and combining. There’s one more operation — splitting — that will help you work more powerfully with strings.
Key ideas
- Using split() method to convert string into a list
Video: Splitting strings
String Split Example
As you saw in the video, the string split
function divides a string into a
list of parts of that string, based on a separator.
groceries = "rice,beans,oil,peppers,meat,greens"
groceries_list = groceries.split(",")
print(groceries_list) #=> ['rice', 'beans', 'oil', 'peppers', 'meat', 'greens']
Exploration
Python has another string function to go the other direction: from a list of strings to a single string, with a separator.
Have you seen that function before? Can you find it by searching the web or the Python docs?