How to Create a List of Items with New Lines in Python?

Estimated read time 1 min read

To create a list of items with new lines in Python, you can define a multi-line string and split it into a list using the splitlines() method. Here’s an example:

items = """apple
banana
orange
grape"""
items_list = items.splitlines()
print(items_list)

Output:

['apple', 'banana', 'orange', 'grape']

In this example, we define a multi-line string variable items containing four fruits separated by new lines. We then call the splitlines() method on items to split the string into a list of individual items. Finally, we print the resulting items_list variable which contains a list of fruits.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply