Search code snippets, questions, articles...

Split string to list python

Python has a built-in function called split() that can be used to split a string into a list of substrings. The split() function takes a string and a delimiter as arguments and returns a list of substrings. The delimiter can be any character, such as a space, a comma, or a tab.
my_str = "This is a string"
print(my_str.split())

comma_str = "Apple, Banana, Orange"
print(comma_str.split(", "))
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

Output

["This", "is", "a", "string"]
["Apple", "Banana", "Orange"]

In the above code examples, the first example splits a string into a list of substrings based on whitespace. The second example does the same but uses a comma and space as the delimiter.

Was this helpful?
0 Comments