Learn How to Split A String in Python with Examples:
At times while working in our programs, we may get a situation where we want to break a string into smaller parts for further processing.
In this tutorial, we will take an in-depth look at String split in Python with simple examples for your easy understanding.
=> Visit Here To Learn Python From Scratch
Table of Contents:
What is ‘String’?
Everything is an Object in Python, hence even String is treated as an object in Python.
The sequence of characters is called String. A character can be anything like symbols, alphabets, numbers etc. The computer doesn’t understand any of these characters or Strings, rather it understands only binary numbers i.e. 0’s and 1’s.
We call this method as encoding and the reverse process is called decoding, and encoding is done based on the ASCII.
Declaring a String
Strings are declared using double quotes (“ “) or single quotes (‘ ‘).
Syntax:
Variable name = “string value”
OR
Variable name = ‘string value’
Example 1:
my_string = “Hello”
Example 2:
my_string = ‘Python’
Example 3:
my_string = “Hello World” print(“String is: “, my_string)
Output:
String is: Hello World
Example 4:
my_string = ‘Hello Python’ print(“String is: “, my_string)
Output:
String is: Hello Python
What is String Split?
As the name itself explains String split means splitting or breaking the given String into smaller pieces.
If you would have worked on Strings in any programming languages, then you might know about concatenation (combining the strings) and String split is just the opposite of it. In order to perform split operations on strings, Python provides us with a built-in function called split().
Python Split function
Python split() method is used to split the string into chunks, and it accepts one argument called separator.
A separator can be any character or a symbol. If no separators are defined, then it will split the given string and whitespace will be used by default.
Syntax:
variable_name = “String value” variable_name.split()
Example 1:
my_string = “Welcome to Python” my_string.split()
Output:
[‘Welcome’, ‘to’, ‘Python’]
How to Split a String in Python?
In the above example, we have used the split() function to split the string without any arguments.
Let’s see some examples of splitting the string by passing some arguments.
Example 1:
my_string = “Apple,Orange,Mango” print(“Before splitting, the String is: “, my_string) value = my_string.split(‘,’) print(“After splitting, the String is: “, value)
Output:
Before the split, the String is: Apple, Orange, Mango
After split, the String is: [‘Apple’, ‘Orange’, ‘Mango’]
Example 2:
my_string = “Welcome0To0Python” print(“Before splitting, the String is: “, my_string) value = my_string.split(‘0’) print(“After splitting, the String is: “, value)
Output:
Before splitting, the String is: Welcome0To0Python
After splitting, the String is: [‘Welcome’, ‘To’, ‘Python’]
Example 3:
my_string = “Apple,Orange,Mango” fruit1,fruit2,fruit3 = my_string.split(‘,’) print(“First Fruit is: “, fruit1) print(“Second Fruit is: “, fruit2) print(“Third Fruit is: “, fruit3)
Output:
First Fruit is: Apple
Second Fruit is: Orange
Third Fruit is: Mango
In the above example, we are splitting the given string “Apple, Orange, Mango” into three parts and assigning these three parts into different variables fruit1, fruit2 and fruit3 respectively.
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.
=> Explore The Simple Python Training Series Here.