102k views
4 votes
Look at the following list:

my_string = 'cookies>milk>fudge>cake>ice cream'
Write a statement that splits this string, creating the following list:
['cookies', 'milk', 'fudge', 'cake', 'ice cream']
Write in python.




10

2 Answers

5 votes

Answer:

my_string = 'cookies>milk>fudge>cake>ice cream'

my_list = my_string.split('>')

print(my_list)

Step-by-step explanation:

The split() method is called on the string my_string with the argument '>', which specifies that the string should be split at each occurrence of the > character. The resulting list is stored in the variable my_list, which can then be printed to verify that it contains the expected values.

User Jahsome
by
7.1k points
2 votes

Answer:

Here's a Python statement that splits the string my_string using the > delimiter and creates a list of strings:

my_string = 'cookies>milk>fudge>cake>ice cream'

my_list = my_string.split('>')

print(my_list)

The split() method is called on the my_string string object, with the > delimiter passed as an argument. This method splits the string at every occurrence of the delimiter and returns a list of the resulting substrings.

The resulting list is stored in the my_list variable, and then printed to the console using the print() function. This should output ['cookies', 'milk', 'fudge', 'cake', 'ice cream'].

User Yuni
by
6.1k points