How to convert a string to a list in python?

A string is a built-in data type in python that represents a sequence of characters. A string is enclosed within a single or double quote. For example, "Python Programming" is a string.

A list is a group of data items enclosed within square brackets []. ['Python', 'Programming'] represents a list. This article discusses different approaches to convert a string to a list in python.

Converting a string to a list of strings

A string can be converted to a list of strings by using split(), a for loop, etc. Let us consider a string string_1 = "Welcome To Python Programming". string_1 when split to a list of strings gives the result as ['Welcome', 'To', 'Python', 'Programming']

Using split() method

The split() method breaks the string by a specified separator and returns a list of strings. It takes two parameters 'sep' and 'maxsplit'.

  • sep represents the separator, The string breaks at this specified separator, if not provided by default white space is considered as a separator.
  • The maxsplit indicates the number of splits to be performed, if not provided it has no limit. Both the parameters sep and maxsplit are optional.

The syntax for a split() method is as follows.

#syntax:
string.split(sep, maxsplit)

The below example illustrates the use of the split() method to convert a string to a list of strings.

string_1 = "Welcome To Python Programming"
print(string_1.split())

Output

['Welcome', 'To', 'Python', 'Programming']

Converting a CSV (comma separated values) to a list

A string with comma-separated values can be converted to a list using split(','). Here, the separator sep takes ',' as a value. The below example illustrates the use of the split() method to convert a CSV to a list.

csv_string = "Welcome,To,Chercher,Tech"
print("csv_string : ", csv_string)
list_1 = csv_string.split(',')
print("list_1 : ", list_1)

Output

csv_string :  Welcome,To,Chercher,Tech
list_1 :  ['Welcome', 'To', 'Chercher', 'Tech']
Using for loop

Two variables temp_str and list_1 are initialized to empty string "" and empty list [] respectively. A for loop iterates over string_1. If the character is not a white space the character is concatenated to temp_str else the temp_str is appended to the list by using append() function and temp_str is again initialized to empty string "".

string_1 = "Welcome To Python Programming"
print("string_1: ", string_1)
temp_str = ""
list_1 = []
for ch in string_1:
    if ch == " ":
        list_1.append(temp_str)
        temp_str = ""
    else:
        temp_str += ch
list_1.append(temp_str)
print("list_1: ", list_1)


The above code return output as

string_1:  Welcome To Python Programming
list_1:  ['Welcome', 'To', 'Python', 'Programming']

Converting a string to a list of characters

The list() constructor provides us the list with individual characters as its elements. A string can be converted into a list of characters using the list() constructor.

For example, consider a string_1 = "Python". list() method returns the list as ['P', 'y', 't', 'h', 'o', 'n']

string_1 = "Python"
print(string_1, string_1)
print("List of characters: ", list(string_1))

Output

List of characters:  ['P', 'y', 't', 'h', 'o', 'n']

Converting a string of integers to a list of integers

Consider a string string_1 = "12345", list() constructor converts the string of integers to list_1 = ['1', '2', '3', '4', '5']. To convert the list of character to list of integers map() function is used.

The map() function has two parameters, a function and an iterable. It passes each data item of the iterable as an argument to the function. map(int, list_1) passes each value of list_1 to the int function.

Example,

string_1 = "12345"
print("string_1 : ", string_1)
list_1 = list(string_1)
print("list_1 : ", list_1)
num_list = list(map(int, list_1))
print("num_list : ", num_list)

Output

string_1 :  12345
list_1 :  ['1', '2', '3', '4', '5']
num_list :  [1, 2, 3, 4, 5]        
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions