Python Split Method with Example
Last updated on 22nd Sep 2020, Artciles, Blog
Python String split() Method
Python split() method splits the string into a comma separated list. It separates strings based on the separator delimiter. This method takes two parameters and both are optional. It is described below.
Signature
- split(sep=None, maxsplit=-1)
Parameters
sep: A string parameter acts as a seperator.
maxsplit: Number of times split perform.
Subscribe For Free Demo
Error: Contact form not found.
Return
It returns a comma separated list.
Examples
Let’s see some examples of split() method to understand it’s functionality.
Example 1
This is a simple example to understand the usage of split() method. No parameter is given, by default whitespaces work as a separator. See the example below.
- # Python split() method example
- # Variable declaration
- str = “Python is a programming language”
- # Calling function
- str2 = str.split()
- # Displaying result
- print(str)
- print(str2)
Output:
Python is a programming language
[‘Python’, ‘is’, ‘a’, ‘programming’, ‘language’]
Example 2
Let’s pass a parameter separator to the method, now it will separate the string based on the separator. See the example below.
- # Python split() method example
- # Variable declaration
- str = “Python is a programming language”
- # Calling function
- str2 = str.split(Python)
- # Displaying result
- print(str2)3
Output:
[”, ‘ is a programming language’]
Example 3
The string is splitted each time when a is occurred. See the example below.
- # Python split() method example
- # Variable declaration
- str = “Python is a programming language”
- # Calling function
- str2 = str.split(‘a’)
- # Displaying result
- print(str)
- print(str2)
Output:
Python is a programming language
[‘Python’, ‘ is ‘, ‘ progr’, ‘mming’, ‘l’, ‘ngu’, ‘ge’]
Split String into List
Whenever we split the string in Python, it will always be converted into List.
As you know, we don’t define any data types in Python, unlike other programming languages. Hence, whenever we use the split() function it’s better that we assign it to some variable so that it can be accessed easily one by one using the advanced for loop.
Example 1:
- my_string = “Apple,Orange,Mango”
- value = my_string.split(‘,’)
for the item in value:
- print(item)
Output:
Apple
Orange
Mango
Split String into Array
As we discussed earlier, whenever we split the string it will always be converted into an Array. However, the way you access data will differ.
Using the split() function, we break the string into some pieces and assign it to some variable, hence using the index we can access the broken strings and this concept is called Arrays.
Let’s see how we can access the split data using arrays.
Example 1:
- my_string = “Apple,Orange,Mango”
- value = my_string.split(‘,’)
- print(“First item is: “, value[0])
- print(“Second item is: “, value[1])
- print(“Third item is: “, value[2])
Output:
First item is: Apple
Second item is: Orange
Third item is: Mango
Tokenize String
When we split the string, it breaks down into smaller pieces and these smaller pieces are called tokens.
Example:
- my_string = “Audi,BMW,Ferrari”
- tokens = my_string.split(‘,’)
- print(“String tokens are: “, tokens)
Output:
String tokens are: [‘Audi’, ‘BMW’, ‘Ferrari’]
In the above example Audi, BMW, and Ferrari are called the tokens of string.
“Audi,BMW,Ferrari”
Split String by Character
In Python, we have an in-built method called list() to split the strings into a sequence of characters.
The list() function accepts one argument which is a variable name where the string is stored.
Syntax:
- variable_name = “String value”
- list(variable_name)
Example:
- my_string = “Python”
- tokens = list(my_string)
- print(“String tokens are: “, tokens)
Output:
String tokens are: [‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’]
Conclusion
We can conclude this tutorial with the following pointers:
- String split is used to break the string into chunks.
- Python provides an in-built method called split() for string splitting.
- We can access the split string by using list or Arrays.
- String split is commonly used to extract a specific value or text from the given string.
Are you looking training with Right Jobs?
Contact Us- Top Python Framework’s
- Python Interview Questions and Answers
- Python Tutorial
- Advantages and Disadvantages of Python Programming Language
- Python Career Opportunities
Related Articles
Popular Courses
- Java Online Training
11025 Learners
- C And C-Plus Plus Online Training
12022 Learners
- Dot Net Training
11141 Learners
- What is Dimension Reduction? | Know the techniques
- Difference between Data Lake vs Data Warehouse: A Complete Guide For Beginners with Best Practices
- What is Dimension Reduction? | Know the techniques
- What does the Yield keyword do and How to use Yield in python ? [ OverView ]
- Agile Sprint Planning | Everything You Need to Know