• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Convert String to List in Python

Convert String to List in Python

Author: Aditya Raj
Last Updated: July 7, 2022

Strings and lists are the most used python objects. Sometimes, while manipulating strings, we need to convert a string to a list. In this article, we will discuss different ways to convert a string to a list in Python.

Table of Contents
  1. Convert String to List Using List() Function in Python
  2. String to List Using append() Method in Python
  3. String to List Using extend() Method in Python
  4. Convert String to List Using List comprehension in Python
  5. Conclusion

Convert String to List Using List() Function in Python

The list() function is used to create a list from an existing iterable object. The list() function takes an iterable object as its input argument. After execution, it returns a list containing all the elements of the iterable object. 

To convert a string to a list in Python, we will pass the input string to the list() function. After execution, it will return a list containing the characters of the input string. You can observe this in the following example.

myStr = "pythonforbeginners"
output_list = list(myStr)
print("The input string is:", myStr)
print("The output list is:", output_list)

Output:

The input string is: pythonforbeginners
The output list is: ['p', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']

In the above example, we have passed the string pythonforbeginners to the list() function. After execution, it has returned the list of characters in the string.

String to List Using append() Method in Python

The append() method is used to append an element to a list. When invoked on a list, it takes an element as its input argument and appends the element to the list.

To convert a string to a list using the append() method, we will use the following steps.

  • First, we will create an empty list named myList to store the output list. 
  • After this, we will iterate through the characters of the input string using a for loop. 
  • During iteration, we will append the current character to myList using the append() method.

After executing the for loop, we will get the output list in myList. You can observe this in the following example.

myStr = "pythonforbeginners"
myList = []
for character in myStr:
    myList.append(character)
print("The input string is:", myStr)
print("The output list is:", myList)

Output:

The input string is: pythonforbeginners
The output list is: ['p', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']

Here, you can observe that we have obtained a list from the input string using a for loop and the append() method.

String to List Using extend() Method in Python

The extend() method is used to add several elements to a list at the same time. The extend() method, when invoked on a list, takes an iterable object as its input argument. After execution, it appends all the elements of the iterable object to the list. 

To convert a string to a list using the extend() method, we will first create an empty list named myList. After that, we will invoke the extend() method on myList with the input string as an input argument to the extend() method. 

After execution of the extend() method, we will get the resultant output in myList. You can observe this in the following example.

myStr = "pythonforbeginners"
myList = []
myList.extend(myStr)
print("The input string is:", myStr)
print("The output list is:", myList)

Output:

The input string is: pythonforbeginners
The output list is: ['p', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']

In the above example, we have obtained the list of characters of the string using the extend() method.

Convert String to List Using List comprehension in Python

List comprehension is used to create a new list from existing iterable objects. The syntax for list comprehension is as follows.

newList=[expression for element in iterable]

To convert a string to a list in python, we will use the input string in the place of iterable. In the place of expression and element, we will use the characters of the string. 

After execution of the above statement, we will get the output list in newList. 

You can observe this in the following example.

myStr = "pythonforbeginners"
myList = [character for character in myStr]
print("The input string is:", myStr)
print("The output list is:", myList)

Output:

The input string is: pythonforbeginners
The output list is: ['p', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']

Conclusion

In this article, we have discussed four ways to convert a string to a list in Python. Out of all these methods, you can use the list() function to create a new list from the input string.

If you want to add the characters of the string to an existing list, you can use the approach with the append() method or the extend() method. To know more about python programming, you can read this article on string concatenation in Python. You might also like this article on dictionary comprehension in Python.

Stay tuned for more informative articles.

Happy Learning!

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Pandas Append Row to DataFrame
  • Convert String to DataFrame in Python
  • Pandas DataFrame to List in Python
  • Solved: Dataframe Constructor Not Properly Called Error in Pandas
  • Overwrite a File in Python

Copyright © 2012–2023 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us