Table of Contents
In this tutorial, we will learn about the Python String split() method with the help of examples.
The split() method breaks up a string at the specified separator and returns a list of strings.
Example
text = 'Python is a fun programming language'
# split the text from space
print(text.split(' '))
# Output: ['Python', 'is', 'a', 'fun', 'programming', 'language']
1. Syntax of String split()
The syntax of split() is:
str.split(separator, maxsplit)
2. split() Parameters
The split() method takes a maximum of 2 parameters:
- separator (optional)- Delimiter at which splits occur. If not provided, the string is splitted at whitespaces.
- maxsplit (optional) – Maximum number of splits. If not provided, there is no limit on the number of splits.
3. split() Return Value
The split() method returns a list of strings.
4. Example 1: How split() works in Python?
text= 'Love thy neighbor'
# splits at space
print(text.split())
grocery = 'Milk, Chicken, Bread'
# splits at ','
print(grocery.split(', '))
# Splits at ':'
print(grocery.split(':'))
Output
['Love', 'thy', 'neighbor'] ['Milk', 'Chicken', 'Bread'] ['Milk, Chicken, Bread']
5. Example 2: How split() works when maxsplit is specified?
grocery = 'Milk, Chicken, Bread, Butter'
# maxsplit: 2
print(grocery.split(', ', 2))
# maxsplit: 1
print(grocery.split(', ', 1))
# maxsplit: 5
print(grocery.split(', ', 5))
# maxsplit: 0
print(grocery.split(', ', 0))
Output
['Milk', 'Chicken', 'Bread, Butter'] ['Milk', 'Chicken, Bread, Butter'] ['Milk', 'Chicken', 'Bread', 'Butter'] ['Milk, Chicken, Bread, Butter']
If maxsplit is specified, the list will have a maximum of maxsplit+1 items.
Related posts:
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python String startswith()
String Hashing
Python Program to Count the Occurrence of an Item in a List
Python Program to Find the Largest Among Three Numbers
Python Program to Check Prime Number
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python String join()
Python bytes()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python Set difference_update()
Python Set update()
Python Program to Get the Full Path of the Current Working Directory
Python Deep Learning Cookbook - Indra den Bakker
Python while Loop
Python Program to Check Armstrong Number
Python Program to Solve Quadratic Equation
Python Program to Find Armstrong Number in an Interval
Python Program to Represent enum
Python Data Structures and Algorithms - Benjamin Baka
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Program to Get the Class Name of an Instance
Python List index()
Python Program to Check Leap Year
Python Program to Check if a Key is Already Present in a Dictionary
String Processing with Apache Commons Lang 3
Python strftime()
Python max()
Python Program Read a File Line by Line Into a List
Jackson – Marshall String to JsonNode
Python filter()