Python String split() method splits a string into a list of strings after breaking the given string by the specified separator.
Example:
Python
string = "one,two,three"
words = string.split(',')
print(words)
Output:
['one', 'two', 'three']
Python String split() Method Syntax
Syntax: str.split(separator, maxsplit)
Parameters
- separator: This is a delimiter. The string splits at this specified separator. If is not provided then any white space is a separator.
- maxsplit: It is a number, that tells us to split the string into a maximum of the provided number of times. If it is not provided then the default is -1 which means there is no limit.
Returns
Returns a list of strings after breaking the given string by the specified separator.
What is the list split() Method?
split() function operates on Python strings, by splitting a string into a list of strings. It is a built-in function in Python programming language.
It breaks the string by a given separator. Whitespace is the default separator if any separator is not given.
How to use list split() method in Python?
Using the list split() method is very easy, just call the split() function with a string object and pass the separator as a parameter. Here we are using the Python String split() function to split different Strings into a list, separated by different characters in each case.
Example: In the above code, we have defined the variable 'text' with the string 'geeks for geeks' then we called the split() method for 'text' with no parameters which split the string with each occurrence of whitespace.
Python
text = 'geeks for geeks'
# Splits at space
print(text.split())
word = 'geeks, for, geeks'
# Splits at ','
print(word.split(','))
word = 'geeks:for:geeks'
# Splitting at ':'
print(word.split(':'))
word = 'CatBatSatFatOr'
# Splitting at t
print(word.split('t'))
Similarly, after that, we applied split() method on different strings with different delimiters as parameters based on which strings are split as seen in the output.
Output['geeks', 'for', 'geeks']
['geeks', ' for', ' geeks']
['geeks', 'for', 'geeks']
['Ca', 'Ba', 'Sa', 'Fa', 'Or']
How does split() work when maxsplit is specified?
The maxsplit parameter is used to control how many splits to return after the string is parsed. Even if there are multiple splits possible, it'll only do maximum that number of splits as defined by the maxsplit parameter.
Example: In the above code, we used the split() method with different values of maxsplit. We give maxsplit value as 0 which means no splitting will occur.
Python
word = 'geeks, for, geeks, pawan'
# maxsplit: 0
print(word.split(', ', 0))
# maxsplit: 4
print(word.split(', ', 4))
# maxsplit: 1
print(word.split(', ', 1))
The value of maxsplit 4 means the string is split at each occurrence of the delimiter, up to a maximum of 4 splits. And last maxsplit 1 means the string is split only at the first occurrence of the delimiter and the resulting lists have 1, 4, and 2 elements respectively.
Output['geeks, for, geeks, pawan']
['geeks', 'for', 'geeks', 'pawan']
['geeks', 'for, geeks, pawan']
How to Parse a String in Python using the split() Method?
In Python, parsing strings is a common task when working with text data. String parsing involves splitting a string into smaller segments based on a specific delimiter or pattern. This can be easily done by using a split() method in Python.
Python
text = "Hello geek, Welcome to GeeksforGeeks."
result = text.split()
print(result)
Explanation: In the above code, we have defined a string 'text' that contains a sentence. By calling the split() method without providing a separator, the string is split into a list of substrings, with each word becoming an element of the list.
Output['Hello', 'geek,', 'Welcome', 'to', 'GeeksforGeeks.']
Hope this tutorial on the string split() method helped you understand the concept of string splitting. split() method in Python has various applications like string parsing, string extraction, and many more. "How to split in Python?" is a very important question for Python job interviews and with this tutorial we have answered the question for you.
Check More: String Methods
For more informative content related to the Python string split() method you can check the following article:
Similar Reads
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
TCP/IP Model The TCP/IP model (Transmission Control Protocol/Internet Protocol) is a four-layer networking framework that enables reliable communication between devices over interconnected networks. It provides a standardized set of protocols for transmitting data across interconnected networks, ensuring efficie
7 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
14 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read