HomepythonHow to Convert a String to a List in Python

How to Convert a String to a List in Python

Converting a string to a list is a common task in Python programming. Whether you’re parsing data, processing user input, or manipulating text, understanding how to effectively transform a string into a list is essential. In this article, we’ll explore various methods to convert a string to a list in Python, each suited for different scenarios.

Basic Conversion Using list()

The most straightforward way to convert a string to a list is to use the list() function. This method splits the string into individual characters, creating a list where each element is a single character from the string.

Example:

string = "hello"
char_list = list(string)
print(char_list)

Output:

['h', 'e', 'l', 'l', 'o']

Splitting a String into Words

If you want to split a string into words or other substrings, the split() method is the way to go. By default, split() divides the string at each whitespace character (spaces, tabs, newlines), but you can also specify a different delimiter.

Example:

sentence = "Convert a string to a list in Python"
word_list = sentence.split()
print(word_list)

Output:

['Convert', 'a', 'string', 'to', 'a', 'list', 'in', 'Python']

You can specify a different delimiter by passing it as an argument to split():

Example:

data = "apple,orange,banana,grape"
fruit_list = data.split(',')
print(fruit_list)

Output:

['apple', 'orange', 'banana', 'grape']

Converting a String of Characters Separated by a Specific Delimiter

In some cases, strings contain characters separated by a specific delimiter, and you may want to convert them into a list of characters. This is particularly common with CSV (Comma-Separated Values) data or other delimited formats.

Example:

char_string = "a,b,c,d,e"
char_list = char_string.split(',')
print(char_list)

Output:

['a', 'b', 'c', 'd', 'e']

Using List Comprehension for Custom Splitting

List comprehension provides a flexible way to convert a string to a list, especially when you need to apply some conditions or transformations to the characters or substrings.

Example:

Suppose you want to convert a string to a list of characters, but only include vowels:

string = "hello world"
vowels = [char for char in string if char in 'aeiou']
print(vowels)

Output:

['e', 'o', 'o']

Advanced: Converting a String to a List of ASCII Values

For more advanced use cases, you might want to convert a string to a list of ASCII values of its characters. This can be done using the ord() function within a list comprehension.

Example:

string = "hello"
ascii_values = [ord(char) for char in string]
print(ascii_values)

Output:

[104, 101, 108, 108, 111]

Converting a string to a list in Python is a versatile operation that can be performed in several ways depending on the requirements:

  • list() function: Converts the string into a list of individual characters.
  • split() method: Splits the string into words or substrings based on whitespace or a specified delimiter.
  • List comprehension: Allows for custom processing and filtering during the conversion.
  • ASCII conversion: Converts the string into a list of ASCII values for each character.
Subscribe
Notify of

0 Comments
Inline Feedbacks
View all comments

Popular