×

Python String split() method

The string.split() method in Python splits a string into a list and returns a list of the words in the string. If the parameter maxsplit is given, at most maxsplit splits are done. If maxsplit is not specified, then there is no limit on the number of splits.

Syntax

split([sep [,maxsplit]])

Parameter

sep(optional) :  This parameter Specifies the separator to use when splitting the string. The default value is whitespace. If the parameter ‘sep’ is not specified or is none, a different splitting algorithm is applied.

Maxsplit(optional) :  This parameter specifies how many splits to do. The Default value is -1, which is "all occurrences".

Return

This method returns a list of the words in the string, using sep as the delimiter string.

Example 1

 # Python program explaining
 # the string.split() method
 # initializing the string
 string = "Python Java C# Ruby"
 print("Original string...")
 print(string)
 # spliting the string when whitespaces ocures
 print("After spliting the string …")
 print(string.split()) 

Output

 Original string...
 Python Java C# Ruby
 After spliting the string …
 ['Python', 'Java', 'C#', 'Ruby'] 

Example 2

 # Python program explaining
 # the string.split() method
 string = "Tutorials and Examples"
 # By default this method Splits at space
 print(string.split()) 
 # Splits at 'g'. As the maximum limit as 1. 
 # So splitting occurs at second t 
 string = "Tutorials, and, Examples"
 print(string.split('t', 1)) 
 # Splitting at '$' with maximum splitting 
 # as 1 
 string = "Tutorials$and$Examples"
 print(string.split('$', 1)) 

Output

 ['Tutorials', 'and', 'Examples']
 ['Tu', 'orials, and, Examples']
 ['Tutorials', 'and$Examples'] 

Example 3

 # Python program explaining
 # the string.split() method
 # initializing the string
 string = "Tutorials, and, Examples"
 # maxsplit: 0 
 print(string.split(', ', 0)) 
 # maxsplit: 4 
 print(string.split(', ', 4)) 
 # maxsplit: 1 
 print(string.split(', ', 1)) 

Output

 ['Tutorials, and, Examples']
 ['Tutorials', 'and', 'Examples']
 ['Tutorials', 'and, Examples'] 

Related Topics

CSV Write in Python

What is meant by CSV? CSV stands for Comma Separated Values. The name itself defines its purpose. CSV arranges the data in the form of tables and stores the organized data in...

3 minutes read.

Python Parallel Processing

By performing more jobs concurrently, your software may complete more tasks in a shorter amount of time. These aid in solving major issues. The following subjects will be covered in...

2 minutes read.

Python program to find factorial of a given number

Python program to find factorial of a given number Factorial is a non-negative integer. It is the product of all positive integers from 1 to the number we are going to...

3 minutes read.

Python Subprocess Call Example

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

4 minutes read.

Python program to count the number of a substring in a string

Python program to count the number of a substring in a string A part of the string is called a substring. This article explains the Python program to find how many...

1 minute read.

Read numpy array in Python

Numpy is a numerical python that deals with multidimensional arrays mostly used in storing multiple values. Python's core scientific computing package is called NumPy. This Python library provides multidimensional array...

9 minutes read.

Python next() function

Python next() function The next() function in Python retrieves the next item from the iterator.  Syntax next(iterator[, default]) Parameter iterable: It is a required parameter which represents an iterable object. default: This parameter represents a default value to...

1 minute read.

Python Virtual Environment

In this tutorial, we will understand Virtual Environment in Python. We will understand the need for a virtual environment and also see how to make use of it in python....

3 minutes read.

Python pynmea2

Define Pynmea2 Python Programming Language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in...

4 minutes read.

Creating Web Application in python

Web Application :  Web Application is an application software that runs in web browser . Software programs will run on operating system. Whereas Web Applications will run on World Wide Web...

5 minutes read.

Application to get live USD/INR rate Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Python MySQL Update Operation

Python MySQL Update Operation: In this part of tutorial, we will learn that how can we update a table present in SQL database through our Python program. As like SQL,...

4 minutes read.

How to build a Virtual Assistant Using Python

What is a virtual assistant? A virtual assistant is a new and very interesting concept in today’s world. When we hear the word “Virtual Assistant”, we can easily visualize “Jarvis or...

8 minutes read.

How to comment out a block of code in Python

When you are writing code in Python, you may want to document it. You need to mention why a piece of code functions, for instance. Mathematics, algorithms, and intricate business...

4 minutes read.

Convert int to Float in Python using Pandas

Introduction We have already learnt about how to convert float variables to int. Now let us know how to convert int variables to float So let us create another Dataset on which...

2 minutes read.

NSE Tools In Python

About NSE NSE (National Stock Exchange) of India Limited is the advanced stock exchange of India. It is located in Mumbai, Maharastra and It was organized in 1992. It was...

2 minutes read.

Selection Sort Using Python

Selection sort is a type of sorting algorithm which is based on sorting elements in increasing order or ascending order through comparison. This sorting technique does not take extra space...

3 minutes read.

Python String swapcase() method

Python String swapcase() method The string.swapcase() method in Python returns a copy of the string with uppercase characters converted to lowercase and vice versa. Syntax string.swapcase() Parameter NA Return This method returns a copy of the string...

1 minute read.

apply() function in python

Pandas: Pandas is a library in python. It is an open source package in python. Pandas in python are used for data cleaning and data analysis. Data frame mainly consists of...

3 minutes read.

Python vs R

Python: - Python is a popular high-level, general-purpose programming language. Python (the most recent version is Python 3) is a programming language used in the software industry for website development, machine...

3 minutes read.