Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
In Python, you can use the str.split()
method to split a string into a list of substrings based on a specified delimiter. By default, split()
uses whitespace (spaces, tabs, and newline characters) as the delimiter.
Here's an example:
text = "This is a sample sentence." word_list = text.split() print(word_list)
This will output:
['This', 'is', 'a', 'sample', 'sentence.']
If you want to split the string using a specific delimiter, you can pass it as an argument to the split()
method:
text = "apple,banana,orange,grape" fruit_list = text.split(',') print(fruit_list)
This will output:
['apple', 'banana', 'orange', 'grape']
In this example, the split()
method is used with a comma (',') as the delimiter, so the string is split at each comma.
Python convert string to list:
my_string = "Hello, World!" my_list = list(my_string) print("String converted to List:", my_list)
How to break a string into a list in Python:
my_string = "Python is amazing" # Method 1: Using list() list_method1 = list(my_string) # Method 2: Using list comprehension list_method2 = [char for char in my_string] print("String broken into List (Method 1):", list_method1) print("String broken into List (Method 2):", list_method2)
Separate string elements into a list in Python:
my_string = "Python" char_list = list(my_string) print("Separated String Elements into List:", char_list)
Splitting a sentence into a list of words in Python:
sentence = "Python is versatile" word_list = sentence.split() print("Sentence split into List of Words:", word_list)
Example code for splitting string into a list:
my_string = "Python,Programming,is,Fun" my_list = my_string.split(',') print("String split into List:", my_list)
Python split string by space into list:
my_string = "Python programming is great" word_list = my_string.split() print("String split by Space into List:", word_list)
Using split() function to create a list from a string in Python:
split()
function to create a list from a string.my_string = "Python is fun" word_list = my_string.split() print("List created using split():", word_list)
Convert comma-separated string to a list in Python:
csv_string = "apple,orange,banana,grape" csv_list = csv_string.split(',') print("Comma-Separated String converted to List:", csv_list)
Python regex for splitting string into a list:
import re my_string = "Python123Programming456Is789Fun" regex_pattern = re.compile(r'\d+') regex_list = re.split(regex_pattern, my_string) print("String split using Regex into List:", regex_list)