Split with comma python

Python String split() Method

Split a string into a list where each word is a list item:

txt = «welcome to the jungle»

Definition and Usage

The split() method splits a string into a list.

You can specify the separator, default separator is any whitespace.

Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

Syntax

Parameter Values

Parameter Description
separator Optional. Specifies the separator to use when splitting the string. By default any whitespace is a separator
maxsplit Optional. Specifies how many splits to do. Default value is -1, which is «all occurrences»

More Examples

Example

Split the string, using comma, followed by a space, as a separator:

txt = «hello, my name is Peter, I am 26 years old»

Example

Use a hash character as a separator:

Example

Split the string into a list with max 2 items:

# setting the maxsplit parameter to 1, will return a list with 2 elements!
x = txt.split(«#», 1)

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Python Split String by Comma

You can split a string in Python with the string formed by the chunks and commas separating them.

In this tutorial, we will learn how to split a string by comma , in Python using String.split().

Examples

1. Split given string by comma

In this example, we will take a string with chunks separated by comma ,, split the string and store the items in a list.

Python Program

str = 'apple,orange,grape' #split string by , chunks = str.split(',') print(chunks)

2. Split given string by one or more commas

If you use String.split() on String with more than one comma coming adjacent to each other, you would get empty chunks. An example is shown below.

Python Program

str = 'apple,,orange. grape' #split string by , chunks = str.split(',') print(chunks)

In this example, we will take a string with chunks separated by one or more underscore characters, split the string and store the chunk in a list, without any empty items.

We shall use re python package in the following program. re.split(regular_expression, string) returns list of items split from string based on the regular_expression.

Python Program

import re str = 'apple,,orange. grape' #split string by , chunks = re.split(',+', str) print(chunks)

Regular Expression ,+ represents one or more commas. So, one or more commas is considered as a delimiter.

One ore more adjacent commas is considered as a single delimiter.

Summary

In this tutorial of Python Examples, we learned how to split a string by comma using String.split() and re.split() methods.

Источник

Python – Split String by Comma

In this tutorial, we will look at how to split a string into a list of strings on the occurrences of a comma in Python with the help of examples.

How to split a string in Python?

Split a string in python by comma

You can use the Python string split() function to split a string (by a delimiter) into a list of strings. To split a string by comma in Python, pass the comma character «,» as a delimiter to the split() function.

📚 Discover Online Data Science Courses & Programs (Enroll for Free)

Introductory ⭐

Intermediate ⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

The following is the syntax –

# split string s by comma s.split(",")

It returns a list of strings resulting from splitting the original string on the occurrences of «,» .

Let’s look at some examples.

Split string by comma

Here, we pass comma «,» as the delimiter to the string split() function.

# string with commas s = "Yes, they backed Steve, Emma, and Rohan in the meeting." # split string by comma ls = s.split(",") print(ls)
['Yes', ' they backed Steve', ' Emma', ' and Rohan in the meeting.']

The resulting list contains words resulting from the split of the original string on occurrences of a comma.

Fix the number of splits

You can also specify the maximum number of splits to be made using the maxsplit parameter. By default, the string split() function makes all the possible splits.

Let’s only split the above string into two parts at the occurrence of a comma, «,» starting from the left. To split the string into two parts, the maxsplit should be 1 , because we’re making only one split resulting in two strings.

# string with commas s = "Yes, they backed Steve, Emma, and Rohan in the meeting." # split string by comma ls = s.split(",", maxsplit=1) print(ls)
['Yes', ' they backed Steve, Emma, and Rohan in the meeting.']

You can see that the resulting list has only two strings.

Let’s look at another example.

Let’s split the original string into three parts, here we pass maxsplit=2 .

# string with commas s = "Yes, they backed Steve, Emma, and Rohan in the meeting." # split string by comma ls = s.split(",", maxsplit=2) print(ls)
['Yes', ' they backed Steve', ' Emma, and Rohan in the meeting.']

The resulting list has only three strings.

You might also be interested in –

Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.

Author

Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects. View all posts

Data Science Parichay is an educational website offering easy-to-understand tutorials on topics in Data Science with the help of clear and fun examples.

Источник

Python String split() Method

Split a string into a list where each word is a list item:

txt = «welcome to the jungle»

Definition and Usage

The split() method splits a string into a list.

You can specify the separator, default separator is any whitespace.

Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

Syntax

Parameter Values

Parameter Description
separator Optional. Specifies the separator to use when splitting the string. By default any whitespace is a separator
maxsplit Optional. Specifies how many splits to do. Default value is -1, which is «all occurrences»

More Examples

Example

Split the string, using comma, followed by a space, as a separator:

txt = «hello, my name is Peter, I am 26 years old»

Example

Use a hash character as a separator:

Example

Split the string into a list with max 2 items:

# setting the maxsplit parameter to 1, will return a list with 2 elements!
x = txt.split(«#», 1)

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Читайте также:  Технология создания html документа
Оцените статью