For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.
It makes sense to start learning by reading and watching videos about fundamentals and how things work.
Data Science and Machine Learning - 16 wks
Full-Stack Software Developer - 16w
Search from all Lessons
Curated list of small interactive and incremental exercises you can take to get better at any coding skill.
Curated section of projects to build while learning with simple instructions, videos, solutions, and more.
Guides on different topics related to the technologies that we teach in our courses
Social & live learning
The most efficient way to learn: Join a cohort with classmates just like you, live streams, impromptu coding sessions, live tutorials with real experts, and stay motivated.
This is how you split a string in python using the
The use of alphanumeric or string-type values is highly necessary for any programming language, as you may already know, these values in Python are called Strings. Knowing how to manipulate this type of String values will be of great use to us in our programming journey since it will allow us to manage the values according to our needs, either to search for a value contained in the Strings, obtain a specific part of it, change certain parts, among others. You can check this article, to learn how to manipulate strings in python, and if you already did in this article, we will dive into the many ways how to split a string in Python
So what is splitting a string? As you read it, it's just taking one string and breaking it into two or more strings, you start with one and end with several other strings.
.split() method, by default, converts a string into a list of strings in which the elements of said list will be the words separated by a space in our original string. Let's see a basic example as follows:
.split() is used as follows:
As you can see, our variable
stringName, which has assigned the value of our original string, was converted into a list. We created another variable called
listName where we assigned the
stringName value alongside the
.split() method to then print the
listName value/results, giving us a new list with the words of our original string.
If we use the
.split() method as it is, it will remove any spaces in our original string and it will take words or letters between the spaces to convert them into list items:
And what if we want to specify which parameter to use to split our strings instead of any whitespace? Or if we want to specify how many splits or divisions we want to do to our strings? Let's look at these cases below.
.split() method accepts two parameters, the
separator and the
maxsplit parameters. These parameters will help us answer our two previous questions. So, the
.split() method can be written as follows:
separator will specify the character where we want to split our original string. Let's say that our original string is being separated by commas instead of spaces, for example:
If we indicate the comma "
," as the separator inside the parentheses of the
.split() method we get:
If we have a space in front of our commas we should also specify the space in our separator:
Otherwise we would get this space in our list of items. And the
maxsplit will specify how many splits we want to perform in our string:
And if we want to make two splits:
.split() method in Python is quite useful for managing and manipulating strings in many situations, there are other methods for separating strings, which work in different ways than the conventional
.split() method called
.rsplit()Method in Python
Unlike the conventional
.split() method, the
.rsplit() method splits the string from the right (the conventional
.split() method does it from the left) at the specified separator and also returns a list of strings that contains the words of our original string. For example:
See that the
.rsplit() method behaves the same way as the conventional
.split() method, but this is only when the
maxsplit parameter is not specified since the method does not have a reference point to do the split. Let's add the
maxsplit parameter to the
.rsplit() method and see how it works:
Here is the difference between the conventional
.split() method and the
.rsplit() method, when a
maxsplit parameter is specified, the
.split() method splits the string starting from the left and the
.rsplit() method splits the string starting from the right.
.splitlines()Method in Python
.splitlines() method is a little bit different, this method splits a string at line breaks and also returns a list of string items that comes from our original string.
This method only accepts a parameter called
keepends, this parameter can be empty, and also can be a boolean (true or false) or a number. This will help us specify whether a line break is included in our resulting list. Let's do an example without the
keepend parameter to see how the
.splitlines() method works:
See how this method removes the line breaks and split the original string into a list of words (other strings). Let's now try to pass a boolean as a
keepend in our
And last but not least, let's pass a number as a
keepend in our
.splitlines() method, in this case, the number 0 will be an equivalent of the False boolean, and any other positive or negative numbers will be an equivalent of the True Boolean. Let's see how this works:
Check 4Geeks to learn more and keep on the Geek Side!