• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Python / Using Python To Split String by Comma into List

Using Python To Split String by Comma into List

October 13, 2022 Leave a Comment

To split a string by comma in Python, you can use the Python string split() function and pass ‘,’ to get a list of strings.

string = "This,is,a,string,with,commas"

print(string.split(","))

#Output:
['This', 'is', 'a', 'string', 'with', 'commas']

You can also use the split() function from the re (regular expression) module.

import re

string = "This,is,a,string,with,commas"

print(re.split(",", string))

#Output:
['This', 'is', 'a', 'string', 'with', 'commas']

When working with strings and text in Python, the ability to manipulate and create new objects from strings can be useful.

One such situation is if you have comma characters in your strings and want to get the substrings between the comma.

To split a string by comma in Python, you can use the Python string split() function and pass ‘,’ to get a list of strings.

Below is a simple example showing you how you can use split() to split a string by comma into a list of strings.

string = "This,is,a,string,with,commas"

print(string.split(","))

#Output:
['This', 'is', 'a', 'string', 'with', 'commas']

Splitting String by Comma with re.split() Function in Python

Another way you can split a string by the comma character is to use the regular expression module split() function to perform a regular expression which will find the “,” characters and then create a list of strings.

Below is a simple example showing you how you can use re.split() to split a string by comma into a list of strings in Python.

import re

string = "This,is,a,string,with,commas"

print(re.split(",", string))

#Output:
['This', 'is', 'a', 'string', 'with', 'commas']

Hopefully this article has been useful for you to learn how to split a string by comma in Python.

Other Articles You'll Also Like:

  • 1.  Python dirname – Get Directory Name of Path using os.path.dirname()
  • 2.  pandas round – Round Numbers in Series or DataFrame
  • 3.  Read Last Line of File Using Python
  • 4.  Add Key Value Pair to Dictionary in Python
  • 5.  Python Infinity – How to Use Infinity in Python
  • 6.  Python issubset() Function – Check if Set is Subset of Another Set
  • 7.  pandas interpolate() – Fill NaN Values with Interpolation in DataFrame
  • 8.  Replace Values in Dictionary in Python
  • 9.  Examples of Recursion in Python
  • 10.  How to Group By Columns and Find Maximum in pandas DataFrame

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy