Python

Python String split() Method

Hello in this tutorial, we will see how to use to string split(…) method in python programming.

1. Introduction

The split() method splits a string into a list after breaking the given string by a specified delimiter. It is represented by the syntax as:

Syntax

string.split(delimiter, maxsplit)

The method accepts:

  • An optional delimiter attribute defines the string splits at the specified separator. If no delimiter is given whitespace is treated as the default
  • An optional maxsplit attribute defines the maximum number of splits for a given string. The default value is -1 meaning no limit on the number of splits

The method returns a list of strings after breaking the given string by the specified delimiter.

1.2 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.

2. Python String split() Method

Let us dive in with the programming stuff now. I am using JetBrains PyCharm as my preferred IDE. You’re free to choose the IDE of your choice.

2.1 Creating an implementation file

Let us understand the implementation with the help of a python script.

Implementation file

# python string split() method
# syntax - str.split(separator, maxsplit)
# where -
# separator - represents the delimiter
# maxsplit - represents the number which tells how many splits to do. default is -1 meaning no limit [optional field]

# splits at space
def space():
    string = 'java code geek'
    print('split string = {}\n'.format(string.split()))


# splits at comma
def comma(delimiter):
    string = 'java,code,geek'
    print('delimiter used = {} and split string = {}\n'.format(delimiter, string.split(delimiter)))


# splits at colon
def colon(delimiter):
    string = 'java:code:geek'
    print('delimiter used = {} and split string = {}\n'.format(delimiter, string.split(delimiter)))


# splits at alphabet
def alphabet(delimiter):
    string = 'javacodegeek'
    print('delimiter used = {} and split string = {}\n'.format(delimiter, string.split(delimiter)))


# when maxsplit is given
# if maxsplit is given the returning value will have a maximum of 'max_split+1' elements
def split(delimiter, maximum_split):
    brands = 'samsung, apple, htc, nokia, sony, lenovo'
    print('delimiter used = {} and split string = {}\n'.format(delimiter, brands.split(delimiter,
                                                                                       maxsplit=maximum_split)))


# driver code
if __name__ == '__main__':
    # space()
    # comma(delimiter=',')
    # colon(delimiter=':')
    # alphabet(delimiter='e')
    # split(delimiter=',', maximum_split=1)

print('done')

You can uncomment the method of your choice and run this python script. If everything goes well the output will be shown in the IDE console.

Console output

split string = ['java', 'code', 'geek']

delimiter used = , and split string = ['java', 'code', 'geek']

delimiter used = : and split string = ['java', 'code', 'geek']

delimiter used = e and split string = ['javacod', 'g', '', 'k']

delimiter used = , and split string = ['samsung', ' apple, htc, nokia, sony, lenovo']

done

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned:

  • Introduction to the python string split(…) method
  • Programming examples

You can download the source code of this tutorial from the Downloads section.

4. More articles

5. Download the Project

This was a tutorial to understand the string split(…) in python programming.

Download
You can download the full source code of this example here: Python String split() Method

Last updated on Jan. 14th, 2022

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button