How do you put all characters of a string into a list in python?

Given a string, write a Python program to split the characters of the given string into a list using Python.

Examples:  

Input : geeks
Output : ['g', 'e', 'e', 'k', 's']

Input : Word
Output : ['W', 'o', 'r', 'd']

Method 1: Split a string into a Python list using unpack(*) method

The act of unpacking involves taking things out, specifically iterables like dictionaries, lists, and tuples.

Python3

string = "geeks"

print([*string])

Output: 

['g', 'e', 'e', 'k', 's']

Method 2: Split a string into a Python list using a loop

Here, we are splitting the letters using the native way using the loop and then we are appending it to a new list.

Python3

string = 'geeksforgeeks'

lst = []

for letter in string:

    lst.append(letter)

print(lst)

Output:

['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']

Method 3: Split a string into a Python list using List Comprehension

This approach uses list comprehension to convert each character into a list. Using the following syntax you can split the characters of a string into a list.

Python3

string = "Geeksforgeeks"

letter = [x for x in string]

print(letter)

Output: 

['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']

Method 4: Split a string into a Python list using a list() typecasting

Python provides direct typecasting of strings into a list using Python list().

Python3

def split(word):

    return list(word)

word = 'geeks'

print(split(word))

Output: 

['g', 'e', 'e', 'k', 's']

Method 5: Split a string into a Python list using extend()

Extend iterates over its input, expanding the list, and adding each member.

Python3

string = 'Geeks@for'

lst = []

lst.extend(string)

print(lst)

Output: 

['G', 'e', 'e', 'k', 's', '@', 'f', 'o', 'r']

In this program, we will try to convert a given string to a list, where spaces or any other special characters, according to the user’s choice, are encountered. To do this we use the split() method in string.

string.split("delimiter")

Examples:

Input : "Geeks for Geeks"
Output : ['Geeks', 'for', 'Geeks']
Input : "Geeks-for-Geeks"
Output : ['Geeks', 'for', 'Geeks']

Method#1: Using split() method

The split method is used to split the strings and store them in the list. The built-in method returns a list of the words in the string, using the “delimiter” as the delimiter string. If a delimiter is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.

Example 1A:

Python3

def Convert(string):

    li = list(string.split(" "))

    return li

str1 = "Geeks for Geeks"

print(Convert(str1))

Output

['Geeks', 'for', 'Geeks']

How do you put all characters of a string into a list in python?

Example 1B:

Python3

def Convert(string):

    li = list(string.split("-"))

    return li

str1 = "Geeks-for-Geeks"

print(Convert(str1))

Output

['Geeks', 'for', 'Geeks']

Method#2: Using string slicing

Python3

def Convert(string):

    list1 = []

    list1[:0] = string

    return list1

str1 = "ABCD"

print(Convert(str1))

Output

['A', 'B', 'C', 'D']

Method#3: Using re.findall() method 

This task can be performed using regular expression. We can use the pattern to match all the alphabet and make list with all the matched elements. 

Python3

import re

def Convert(string):

    return re.findall('[a-zA-Z]', string)

str1="ABCD"

print("List of character is : ",Convert(str1))

Output

List of character is :  ['A', 'B', 'C', 'D']

Method #4: Using list comprehension 

Python3

s="Geeks"

x=[i for i in s]

print(x)

Output

['G', 'e', 'e', 'k', 's']

Method #5: Using enumerate function 

Python3

s="geeks"

x=[i for a,i in enumerate(s) ]

print(x)

Output

['g', 'e', 'e', 'k', 's']

Method #6: Using JSON

Python3

import json

stringA = '["geeks", 2,"for", 4, "geeks",3]'

res = json.loads(stringA)

print("The converted list : \n",res)

Output

The converted list : 
 ['geeks', 2, 'for', 4, 'geeks', 3]

Method #7: Using ast.literal

Python3

import ast

ini_list = '["geeks", 2,"for", 4, "geeks",3]'

res = ast.literal_eval(ini_list)

print(res)

print(type(res))

Output

['geeks', 2, 'for', 4, 'geeks', 3]
<class 'list'>

Method #8: Using lambda function

Python3

s="Geeks"

x=list(filter(lambda i:(i in s),s))

print(x)

Output

['G', 'e', 'e', 'k', 's']

Method #9: Using map() 

Python3

s="Geeks"

x=list(map(str,s))

print(x)

Output

['G', 'e', 'e', 'k', 's']

Method#10: Using list()

Python3

s = "Geeks"

x = list(s)

print(x)

Output:

['G', 'e', 'e', 'k', 's']

How do you make each letter of a string into a list?

How to Convert a String to a List of Words. Another way to convert a string to a list is by using the split() Python method. The split() method splits a string into a list, where each list item is each word that makes up the string. Each word will be an individual list item.

How do I convert a string to a list in Python?

To convert string to list in Python, use the string split() method. The split() is a built-in method that splits the strings and stores them in the list.

How do I convert a string to a list of strings?

You can concatenate a list of strings into a single string with the string method, join() . Call the join() method from 'String to insert' and pass [List of strings] . If you use an empty string '' , [List of strings] is simply concatenated, and if you use a comma , , it makes a comma-delimited string.

How do you make a list of letters in Python?

How to make a list of the alphabet in Python.
alphabet_string = string. ascii_lowercase. Create a string of all lowercase letters..
alphabet_list = list(alphabet_string) Create a list of all lowercase letters..
print(alphabet_list).