Open In App

Python String split()

Last Updated : 20 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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']

Time Complexity: O(n)
Auxiliary Space: O(n)

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']

Time Complexity: O(n)
Auxiliary Space: O(n)

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:

Python String split() – FAQs

What does split('\t') do in Python?

In Python, the split('\t') method splits a string into a list of substrings based on the tab (\t) delimiter. Here’s how it works:

text = "apple\tbanana\torange"
result = text.split('\t')
print(result) # Output: ['apple', 'banana', 'orange']

In this example, the split('\t') method divides the string text wherever it encounters a tab character (\t) and returns a list containing the separated substrings.

What is input().split() in Python?

input() is a built-in function in Python that reads a line from input, which is typically from the user via the console. split() is a method that splits a string into a list of substrings based on whitespace by default, or a specified delimiter. Together, input().split() allows you to read user input and split it into individual components based on whitespace.

Example:

# User input: "apple banana orange"
words = input().split()
print(words) # Output: ['apple', 'banana', 'orange']

Here, input() reads the input from the user, and split() divides the input into a list of words based on whitespace.

How to split a number in Python?

To split a number (typically an integer or float) into its individual digits, you can convert the number to a string and then split the string. Here’s an example:

number = 12345
digits = list(str(number))
print(digits) # Output: ['1', '2', '3', '4', '5']

In this example, str(number) converts the integer 12345 into a string, and list() converts the string into a list of individual characters (‘1’, ‘2’, ‘3’, ‘4’, ‘5’).

How to split a string into lines using the split() method?

To split a multi-line string into individual lines using the split() method, you can specify the newline character (\n) as the delimiter. Here’s an example:

multiline_text = "Line 1\nLine 2\nLine 3"
lines = multiline_text.split('\n')
print(lines) # Output: ['Line 1', 'Line 2', 'Line 3']

In this example, split('\n') splits the multiline_text string wherever it encounters a newline character (\n) and returns a list of lines.

How to split a string number?

If by “split a string number” you mean splitting a string representation of a number into its individual characters or parts, you can use the split() method with an empty string as the delimiter. Here’s an example:

number_str = "12345"
digits = list(number_str)
print(digits) # Output: ['1', '2', '3', '4', '5']

In this example, list(number_str) converts the string "12345" into a list of individual characters (‘1’, ‘2’, ‘3’, ‘4’, ‘5’).



Similar Reads

Python | Pandas Split strings into two List/Columns using str.split()
Pandas provide a method to split string around a passed separator/delimiter. After that, the string can be stored as a list in a series or it can also be used to create multiple column data frames from a single separated string. It works similarly to Python's default split() method but it can only be applied to an individual string. Pandas <code
4 min read
Python program to split and join a string
Python program to Split a string based on a delimiter and join the string using another delimiter. Splitting a string can be quite useful sometimes, especially when you need only certain parts of strings. A simple yet effective example is splitting the First-name and Last-name of a person. Another application is CSV(Comma Separated Files). We use s
7 min read
Python | Split given string into equal halves
Sometimes, we need to simply divide the string into two equal halves. This type of application can occur in various domain ranging from simple programming to web development. Let's discuss certain ways in which this can be performed. Method #1 : Using list comprehension + String slicing This is the naive method to perform this particular task. In t
4 min read
Split a string in equal parts (grouper in Python)
Grouper recipe is an extended toolset made using an existing itertool as building blocks. It collects data into fixed-length chunks or blocks. Existing Itertools Used: izip_longest(*iterables[, fillvalue]) : Make an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are filled-in with
2 min read
Python | Split string into list of characters
Given a string, write a Python program to split the characters of the given string into a list using Python. In this article, we will explore various methods to split a string into a list of characters, allowing developers to manipulate and work with individual characters efficiently. Input: geeks Output : ['g', 'e', 'e', 'k', 's'] Input: Word Outp
4 min read
Python | Split string in groups of n consecutive characters
Given a string (be it either string of numbers or characters), write a Python program to split the string by every nth character. Examples: Input : str = "Geeksforgeeks", n = 3 Output : ['Gee', 'ksf', 'org', 'eek', 's'] Input : str = "1234567891234567", n = 4 Output : [1234, 5678, 9123, 4567] Method #1: Using list comprehension C/C++ Code # Python
2 min read
Python | String Split including spaces
The problems and at the same time applications of list splitting are quite common while working with python strings. The spaces usually tend to ignore in the use cases. But sometimes, we might not need to omit the spaces but include them in our programming output. Let's discuss certain ways in which this problem can be solved. Method #1: Using spli
4 min read
Python | Split CamelCase string to individual strings
Given a string in a camel-case, write a Python program to split each word in the camel case string into individual strings. Examples: Input : "GeeksForGeeks" Output : ['Geeks', 'For', 'Geeks'] Input : "ThisIsInCamelCase" Output : ['This', 'Is', 'In', 'Camel', 'Case'] Method #1: Naive Approach A naive or brute-force approach to split CamelCase strin
4 min read
Python | Split string on Kth Occurrence of Character
Many problems of the split have been dealt with in the past, but sometimes, one can also encounter this problem in which we need to split the string on the Kth occurrence of a character. Let's discuss certain ways in which this problem can be solved. Method #1 : Using split() + join() The split and join method can perform this particular task. In t
6 min read
Python | Ways to split a string in different ways
The most common problem we have encountered in Python is splitting a string by a delimiter, But in some cases we have to split in different ways to get the answer. In this article, we will get substrings obtained by splitting string in different ways. Examples: Input : Paras_Jain_Moengage_best Output : ['Paras', 'Paras_Jain', 'Paras_Jain_Moengage',
2 min read
Article Tags :
Practice Tags :