Split a String into a List in Python

In Python, you can split a string into a list using the split() method. This method divides a string into multiple substrings based on a specified delimiter (such as spaces or commas) and returns them as a list. The default separator is whitespace if no delimiter is provided.


Examples

1. Splitting a String by Spaces

The simplest way to split a string is by using the split() method without any parameters. This splits the string based on whitespace characters (spaces, tabs, or newlines).

</>
Copy
# Given string
text = "Python is a powerful language"

# Splitting the string into a list of words
words_list = text.split()

# Printing the result
print("List of words:", words_list)

Explanation:

Here, the string text contains words separated by spaces. The split() method without arguments automatically splits the string wherever there is whitespace, storing the resulting words in the list words_list.

Output:

List of words: ['Python', 'is', 'a', 'powerful', 'language']

2. Splitting a String by a Specific Delimiter

You can specify a custom delimiter to split the string. For example, if the string contains commas, you can split it using split(",").

</>
Copy
# Given string with commas
csv_text = "apple,banana,grape,orange"

# Splitting the string using ',' as the delimiter
fruits_list = csv_text.split(",")

# Printing the result
print("List of fruits:", fruits_list)

Explanation:

The string csv_text contains words separated by commas. Using split(","), Python breaks the string at each comma and stores the resulting parts in the list fruits_list.

Output:

List of fruits: ['apple', 'banana', 'grape', 'orange']

3. Splitting a String with a Maximum Number of Splits

The split() method also allows limiting the number of splits by passing a second argument.

</>
Copy
# Given string
sentence = "Python is easy to learn and use"

# Splitting the string with max splits set to 3
split_limited = sentence.split(" ", 3)

# Printing the result
print("Limited split list:", split_limited)

Explanation:

The string sentence is split using spaces, but only up to the first 3 occurrences (defined by the second argument of split()). The remaining part of the string stays as the last element in the list.

Output:

Limited split list: ['Python', 'is', 'easy', 'to learn and use']

4. Splitting a String by Newline Characters

If a string contains multiple lines, you can split it using the newline character \n.

</>
Copy
# Given multiline string
multiline_text = "Hello\nWelcome to Python\nEnjoy coding!"

# Splitting the string at newline characters
lines_list = multiline_text.split("\n")

# Printing the result
print("List of lines:", lines_list)

Explanation:

The string multiline_text contains multiple lines separated by newline characters (\n). Using split("\n"), the string is broken down into separate lines and stored in lines_list.

Output:

List of lines: ['Hello', 'Welcome to Python', 'Enjoy coding!']

5. Splitting a String into Individual Characters

If you want to split a string into individual characters, you can use list() instead of split().

</>
Copy
# Given string
word = "Python"

# Splitting into individual characters
char_list = list(word)

# Printing the result
print("List of characters:", char_list)

Explanation:

The string word is converted into a list of its individual characters using the list() function. Each character in the original string is stored as a separate element in the list char_list.

Output:

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

Conclusion

In Python, you can split a string into a list using different methods:

  1. split() without arguments: Splits by whitespace.
  2. split(delimiter): Splits based on a custom delimiter like a comma or newline.
  3. split(delimiter, maxsplit): Limits the number of splits.
  4. list(): Converts a string into a list of characters.