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.
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
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']
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’]
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 Chennai, Python Course Bangalore, Python Course Dallas, Python 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.
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:
Name | Dates | |
---|---|---|
Python Training | Dec 31 to Jan 15 | View Details |
Python Training | Jan 04 to Jan 19 | View Details |
Python Training | Jan 07 to Jan 22 | View Details |
Python Training | Jan 11 to Jan 26 | View Details |
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.