×

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

Python Web Development projects

What is Python? Python is currently one of the most widely used computer programming languages. This isn't just an exaggeration; Python is the second-most famous programming language on the software development...

3 minutes read.

Python String isspace() method

Python String isspace() method The string.isspace() method returns a Boolean value true if there are only whitespace characters in the given string. This function is used to check if the given...

2 minutes read.

Python isinstance() function

Python isinstance() function The isinstance() function in Python returns a Boolean value ‘True’ if the given object is of the specified type, otherwise it returns False. Syntax isinstance(object, classinfo) Parameter object: It is a required parameter which represents an object. classinfo: This...

1 minute read.

Applications of Python

Top 10 Applications of Python in 2020- 2021 Python language is famous for its general-purpose nature, which enables developers to use it in every field of development. Python can be found...

6 minutes read.

What is a Script in Python

Have you ever heard that Python is a scripting language? Or when people address a Python program file as a script? Besides programming, script generally means "a written story for...

3 minutes read.

Python Operator Precedence

Before knowing about the operator precedence in Python, we have to know about the operators in Python. So let's have a look at it. According to one definition, the operator is...

3 minutes read.

Python SQLite

SQLite It is an RDBMS (Relational Database Management System). It is an embedded, serverless, transactional SQL database engine. It is an open-source application. It is named SQLite because of its lightweight....

3 minutes read.

Programs for Printing Pyramid Patterns in Python

<!-- wp:paragraph --><p>Python supports printing patterns using basic for loops. The number of rows is handled by the first outer loop, while the number of columns is handled by the...

9 minutes read.

Best resources to learn Numpy and Pandas in python

In this tutorial, we will discuss the different resources where we can learn about NumPy and Pandas libraries of Python in a more efficient way. NumPy NumPy, a Python library used for...

4 minutes read.

XXhash Python

Python: 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 is said...

6 minutes read.

Python Command line Arguments

Python Command line Arguments The command-line argument is used to the change functionality of the program. It's an extra command the programmer can use while launching a program. These commands have many uses...

7 minutes read.

ER diagram of the Bank Management System in python

What is an ER diagram? The full form of the ER diagram is the Entity Relationship diagram. This diagram is used in database management systems to have a rough idea of...

3 minutes read.

Iterate a Dictionary in Python – Part 2

In this tutorial, we will learn above various methods used to Iterate a Dictionary in Python. Dictionary: In Python, a dictionary is an unordered collection of data values that is used to...

3 minutes read.

Python sort() function

Python provides many built-in functions for solving many problems that arise in different situations in programs. One of such methods is the sort () method. In this article, the syntax...

4 minutes read.

Python Set isdisjoint() method

Python Set isdisjoint() method The set.isdisjoint() method in Python returns a boolean value True if two sets are disjoint sets ( i.e. none of the elements are present in both sets), otherwise it returns...

1 minute read.

How to open a file in python

Opening a File in Python Python is a user-friendly programming language that makes almost every concept easy. It provides many inbuilt functions in its libraries to work with files. Using these...

6 minutes read.

Data Distribution in python

Before discussing data distribution, we will discuss each word that is data and Distribution. What is meant by data? A collection of raw facts and figures is called data. The word "raw"...

18 minutes read.

Python email utils

Python is a popular programming language in this growing world. There are many resources to learn python online without spending a single penny. In this article, we will talk about a...

4 minutes read.

Notepad++ For Python

In this article, you are going to learn what notepad++ is and how it is integrated with python to use the python programming language. You can also learn how to...

4 minutes read.

Converting Set to List in Python

Converting ‘set’ datatype to ‘list’ datatype is called typecasting. Typecasting in programming is a method to convert one datatype into another datatype. It may happen implicitly by the defined language, called...

3 minutes read.