• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
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 Prime Factorization – Find Prime Factors of Number
  • 2.  Python Prepend to List with insert() Function
  • 3.  Return Multiple Values from Function in Python
  • 4.  Get Year from Date in pandas DataFrame
  • 5.  Get Random Letter in Python
  • 6.  Squaring in Python – Square a Number Using Python math.pow() Function
  • 7.  How to Write CSV File to AWS S3 Bucket Using Python
  • 8.  How to Read Pickle File from AWS S3 Bucket Using Python
  • 9.  pandas min – Find Minimum Value of Series or DataFrame
  • 10.  PROC PHREG Equivalent in Python

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 *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

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

x