![]() |
Creating a list of strings is a common task in Python programming, and there are several straightforward methods to achieve this. In this article, we will explore some simple and generally used methods to create a list of strings and print the results. Create a List of Strings in PythonBelow, are the methods to Create A List Of Strings in Python.
Create A List Of Strings Using Square BracketsThe most basic and widely used method to create a list of strings is by using square brackets. Simply enclose the strings within square brackets, separated by commas. Python3
Output
Result: ['apple', 'banana', 'cherry', 'date', 'elderberry'] Create A List Of Strings Using list() constructorThe Python3
Output
Result: ['apple', 'banana', 'cherry', 'date', 'elderberry'] Create A List Of Strings Using List ComprehensionList comprehension provides a concise and readable way to create a list. It allows you to apply an expression to each item in an iterable. Python3
Output
Result: ['Apple', 'Banana', 'Cherry', 'Date', 'Elderberry'] Create A List Of Strings Using extend() MethodThe Python3
Output
Result: ['apple', 'banana', 'cherry', 'date', 'elderberry'] ConclusionThese five methods provide versatile ways to create a list of strings in Python. Whether you prefer the simplicity of square brackets, the flexibility of the |
Reffered: https://www.geeksforgeeks.org
Python Programs |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |