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

Split a string into a list in Python

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.

  1. Python convert string to list:

    • Description: Converting a string into a list in Python.
    • Code:
      my_string = "Hello, World!"
      my_list = list(my_string)
      print("String converted to List:", my_list)
      
  2. How to break a string into a list in Python:

    • Description: Breaking a string into a list using various methods.
    • Code:
      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)
      
  3. Separate string elements into a list in Python:

    • Description: Separating individual characters of a string into a list.
    • Code:
      my_string = "Python"
      char_list = list(my_string)
      print("Separated String Elements into List:", char_list)
      
  4. Splitting a sentence into a list of words in Python:

    • Description: Splitting a sentence into a list of words.
    • Code:
      sentence = "Python is versatile"
      word_list = sentence.split()
      print("Sentence split into List of Words:", word_list)
      
  5. Example code for splitting string into a list:

    • Description: Providing a basic example of splitting a string into a list in Python.
    • Code:
      my_string = "Python,Programming,is,Fun"
      my_list = my_string.split(',')
      print("String split into List:", my_list)
      
  6. Python split string by space into list:

    • Description: Splitting a string by space into a list.
    • Code:
      my_string = "Python programming is great"
      word_list = my_string.split()
      print("String split by Space into List:", word_list)
      
  7. Using split() function to create a list from a string in Python:

    • Description: Using the split() function to create a list from a string.
    • Code:
      my_string = "Python is fun"
      word_list = my_string.split()
      print("List created using split():", word_list)
      
  8. Convert comma-separated string to a list in Python:

    • Description: Converting a comma-separated string into a list.
    • Code:
      csv_string = "apple,orange,banana,grape"
      csv_list = csv_string.split(',')
      print("Comma-Separated String converted to List:", csv_list)
      
  9. Python regex for splitting string into a list:

    • Description: Using regex to split a string into a list based on a pattern.
    • Code:
      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)