Day 79/150 – Convert String to List in Python
Strings are one of the most commonly used data types in Python. Sometimes, you need to convert a string into a list to process individual characters or words. Python provides several simple ways to achieve this depending on your use case.
In this post, we'll explore four easy methods to convert a string into a list.
Method 1 – Using list() (Convert to Character List)
The list() function converts each character of the string into an individual list element.
text = "Python" result = list(text) print(result)
Output:
['P', 'y', 't', 'h', 'o', 'n']
Explanation:
- list() treats the string as an iterable.
- Every character becomes a separate element in the list.
- This is the easiest method when working with characters.
Method 2 – Taking User Input
You can also convert a user-entered string into a list of characters.
Pythontext = input("Enter a string: ") result = list(text) print(result)
Sample Input:
Output:
['P', 'y', 't', 'h', 'o', 'n']Explanation:
- Accepts input from the user.
- Converts every character into a list item.
Method 3 – Using split() (Convert to Word List)
If you want to split a sentence into words instead of characters, use the split() method.
text = "Python is easy to learn" result = text.split() print(result)
Output:
['Python', 'is', 'easy', 'to', 'learn']
Explanation:
- split() separates the string based on spaces by default.
- Each word becomes a separate list element.
- Ideal for processing sentences.
Method 4 – Using List Comprehension
List comprehension provides a concise way to create a character list.
['P', 'y', 't', 'h', 'o', 'n']text = "Python" result = [ch for ch in text] print(result)
Output:
Explanation:
- Iterates through every character.
- Adds each character to a new list.
- Easy to customize with conditions if needed.
Comparison of Methods
| Method | Best For |
|---|---|
| list() | Convert string into characters |
| User Input + list() | Interactive programs |
| split() | Convert sentence into words |
| List Comprehension | Custom character processing |
Conclusion
Converting a string into a list is a common Python operation. If you need individual characters, use list() or list comprehension. If you're working with sentences, split() is the best choice. Choose the method based on whether you need characters or words.
Keep practicing—small concepts like these build a strong Python foundation!
#Python #PythonProgramming #Coding #LearnPython #100DaysOfCode #Programming #Developers #PythonTips #CodingChallenge #CodeNewbie
.png)


