Python Split Method with Example

What is Split() Method in Python?

If you want to break a large string into a small number of strings (or) chunks, you can use string split() method in python. This method is totally different concatenation, which is used to merge and combine strings into one.

This split function divides the string into splits and adds data to the array string with the help of the defined separator. If there is no separator defined when you call the split function, by default, white space is used.

If you would like to become a Python certified professional, then visit Mindmajix - A Global online training platform:  Python Training”  Course.  This course will help you to achieve excellence in this domain.

Parameters of split:

Separator: Separator is defined as a character which is placed between every variable.

Max Split: It is a parameter in which the number is given that as the maximum number of splits should be done in a string. If this max split is not provided there is no limit for the splitting of a string.

Related Article: Python Array Examples

The default max limit will be “-1”, which is known as all occurrences.

 Syntax:

str.split(separator, maxsplit)

Result: 

The result will be in the form of breakdown strings from the given string with the specified separator.

Frequently Asked Python Interview Questions & Answers

Lets see some examples of how the split() function is defined in different types in python:

Split the string using space

txt =  " hi my name is David"
a  = txt.split()
print(a)

Split the string using the comma

txt   =   (" tea, coffee, milk") 
a  = text.split(",")
print(a)

Related Article: Python String Functions

Split the string using Hash(#)

txt  = "#orange#strawberry#grapes#banana"
a   = text.split("#")
print(a)

Output

['hi ', 'my name is David']
['tea',  'coffe ', 'milk' ]
['orange', 'strawberry', 'grapes', 'banana']

 MindMajix YouTube Channel

Split the string using Max split

txt    =  “hi, my name is David, I am a blogger”

#maxsplit  3
print(txt.split(‘ , ‘ ,3))

#maxsplit 2
print(txt.split(‘ , ‘ , 2))

#maxsplit  1
print(txt.split(‘, ‘ , 1))

#maxsplit  0
print(txt.split(‘. ‘, 0)

Output

[ ‘‘hi’, ‘ my name is David’, ‘I am a blogger’]
            [ ‘‘hi’, ‘my name is David’,   I am a blogger’ ]
            [ ‘‘hi’, my name is David, I am a blogger’ ]
            [‘hi, my name is David, I am a blogger’]

Conclusion

In this article, we have seen why the split() method is used in Python programming language and in how many cases split can be used. Hope you are clear with the concepts after going through this article. Please does comment if you have any suggestions.

If you are interested to learn Python and to become an Python Expert? Then check out our Python Certification Training Course at your near Cities.

Python Course ChennaiPython Course BangalorePython Course DallasPython Course Newyork

These courses are incorporated with Live instructor-led training, Industry Use cases, and hands-on live projects. This training program will make you an expert in Python and help you to achieve your dream job.

Explore Python Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
Python TrainingNov 19 to Dec 04View Details
Python TrainingNov 23 to Dec 08View Details
Python TrainingNov 26 to Dec 11View Details
Python TrainingNov 30 to Dec 15View Details
Last updated: 03 Apr 2023
About Author

Anjaneyulu Naini is working as a Content contributor for Mindmajix. He has a great understanding of today’s technology and statistical analysis environment, which includes key aspects such as analysis of variance and software,. He is well aware of various technologies such as Python, Artificial Intelligence, Oracle, Business Intelligence, Altrex, etc. Connect with him on LinkedIn and Twitter.

read less
  1. Share:
Python Articles