-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
52 changed files
with
2,246 additions
and
761 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,230 +1,103 @@ | ||
--- | ||
title: "How to Split a String in Python" | ||
subtitle: "Knowing how to manipulate strings this way will be of great use to us in our programming journey. In this article, we will dive into the many ways how to split a string in Python." | ||
title: "How to Split a String in Python?" | ||
subtitle: "Learn different methods to split a string into substrings in Python. Explore practical examples and best practices for effective string manipulation." | ||
tags: ["python","string concatenation",] | ||
date: "2020-10-19T16:36:30+00:00" | ||
authors: [] | ||
status: "published" | ||
authors: [tommygonzaleza] | ||
|
||
--- | ||
|
||
This is how you split a string in python using the **`.split()`** method: | ||
On this article we'll be helping you with one of the most common tasks when [working with strings in Python](https://4geeks.com/lesson/working-with-strings-in-python), providing you different methods to split a string in Python. This is a quick example on how you can split a string in python by using the `.split()` method: | ||
|
||
```py | ||
string1 = "This string will be splitted" | ||
listName = string1.split() | ||
```py runable=true | ||
text = "This string will be splitted" | ||
list_name = text.split() | ||
|
||
print(listName) #Output: ["This", "string", "will", "be", "splitted"] | ||
print(list_name) #Output: ["This", "string", "will", "be", "splitted"] | ||
``` | ||
|
||
## How to Split a String in Python | ||
In the previous example, we used the `split()` method with no parameters, to get our `text` split by the empty spaces into substrings. | ||
|
||
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](https://4geeks.com/lesson/working-with-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** | ||
## How to split a string in Python using the split method? | ||
|
||
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. | ||
[Python](https://4geeks.com/lesson/what-is-python-used-for) provides a powerful method called `split`, which allows you to split a string into a list of substrings based on a specified separator. This method is extremely useful when you need to break down a string into its different parts. | ||
|
||
## How to Split a String in Python, **`.split()`** Method | ||
## Parameters of the split method | ||
|
||
The `.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: | ||
The `split()` method in Python accepts several parameters, let's explore them and see how they work. | ||
|
||
```py | ||
stringName = "My name is Rigoberto" | ||
```python | ||
string.split( | ||
sep=None, | ||
maxsplit=-1 | ||
) | ||
``` | ||
|
||
The `.split()` is used as follows: | ||
- `sep`: The `sep` parameter is used to specify the separator based on which the string should be split. If no separator is provided, whitespace characters (space, tab, newline, etc.) are used by default to split the string. | ||
- `maxsplit`: This parameter determines the maximum number of splits that can occur. By default, it is set to `-1`, which means there is no limit to the number of splits that can occur. If a positive integer is provided, the string will be split at most that many times, and the remainder of the string will be returned as the final element of the list. | ||
|
||
```py | ||
stringName = "My name is Rigoberto" | ||
listName = stringName.split() | ||
Let's break down each of these parameters: | ||
|
||
print(listName) #Output: ["My", "name", "is", "Rigoberto"] | ||
``` | ||
|
||
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: | ||
|
||
- **Example 1: with more spaces in `stringName`** | ||
```py | ||
stringName = "My name is Rigoberto" | ||
listName = stringName.split() | ||
|
||
print(listName) #Output: ["My", "name", "is", "Rigoberto"] (same result as before) | ||
``` | ||
|
||
- **Example 2: spacing between letters** | ||
|
||
```py | ||
stringName = "M y na me is Rigoberto" | ||
listName = stringName.split() | ||
|
||
print(listName) #Output: ["M", "y", "na" , "me", "is", "Rigoberto"] | ||
``` | ||
|
||
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. | ||
|
||
|
||
|
||
## Syntax of the **`.split()`** Method | ||
|
||
The `.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: | ||
|
||
```py | ||
.split(separator, maxsplit) | ||
``` | ||
|
||
The `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: | ||
|
||
```py | ||
stringName = "My,name,is,Rigoberto" | ||
``` | ||
|
||
If we indicate the comma "`,`" as the separator inside the parentheses of the `.split()` method we get: | ||
|
||
```py | ||
listName = stringName.split(",") | ||
|
||
print(listName) #Output: ["My", "name", "is", "Rigoberto"] | ||
``` | ||
|
||
If we have a space in front of our commas we should also specify the space in our separator: | ||
|
||
```py | ||
stringName = "My, name, is, Rigoberto" | ||
listName = stringName.split(", ") | ||
|
||
print(listName) #Output: ["My", "name", "is", "Rigoberto"] | ||
``` | ||
Using the `split()` method effectively can help you manipulate and process strings efficiently in Python. | ||
|
||
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: | ||
## Examples of how to use the split Method in Python | ||
|
||
The `split()` method in Python is incredibly versatile, allowing you to split strings based on specified separators. Let's explore five examples showcasing its different usages: | ||
|
||
```py | ||
stringName = "My,name,is,Rigoberto" | ||
listName = stringName.split(",", 1) | ||
### Splitting a String by Whitespace | ||
|
||
print(listName) #Output: ["My", "name,is,Rigoberto"] | ||
``` | ||
|
||
And if we want to make two splits: | ||
```python runable=true | ||
text = "This is a sample sentence." | ||
words = text.split() | ||
|
||
```py | ||
stringName = "My,name,is,Rigoberto" | ||
listName = stringName.split(",", 2) | ||
|
||
print(listName) #Output: ["My", "name", "is,Rigoberto"] | ||
print(words) | ||
``` | ||
|
||
In this example, the `split()` method is called without any arguments, which means it splits the string `text` into words based on whitespace. The resulting list `words` will contain each word from the sentence as individual elements. | ||
|
||
#### **Related: [Why 4Geeks Academy teaches Python as Back End Language?](https://4geeksacademy.com/us/python-bootcamp/why-we-teach-python-4geeks)** | ||
|
||
## How to Split a String in Python. Alternatives Methods | ||
|
||
Although the `.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()` and `.splitlines()`. | ||
|
||
|
||
### `.rsplit()` Method in Python | ||
### Splitting a String by a Custom Separator | ||
|
||
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: | ||
```python runable=true | ||
numbers = "1,2,3,4,5" | ||
number_list = numbers.split(",") | ||
|
||
- **Example 1** | ||
|
||
```py | ||
stringName = "My name is Rigoberto" | ||
listName = stringName.rsplit() | ||
|
||
print(listName) #Output: ["My", "name", "is", "Rigoberto"] | ||
``` | ||
|
||
- **Example 2** | ||
```py | ||
stringName = "My,name,is,Rigoberto" | ||
listName = stringName.rsplit(",") | ||
|
||
print(listName) #Output: ["My", "name", "is", "Rigoberto"] | ||
print(number_list) | ||
``` | ||
|
||
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, we split the string `numbers` by commas (`,`) using the `split()` method with a comma as the separator. The resulting list `number_list` will contain each number as an individual element. | ||
|
||
- **Using the conventional `.split()` method** | ||
### Limiting the Number of Splits | ||
|
||
```py | ||
stringName = "My,name,is,Rigoberto" | ||
listName = stringName.split(",", 1) | ||
```python runable=true | ||
address = "123 Main Street, City, Country" | ||
components = address.split(",", 1) | ||
|
||
print(listName) #Output: ["My", "name,is,Rigoberto"] | ||
print(components) | ||
``` | ||
|
||
- **Using the `.rsplit()` method**: | ||
|
||
```py | ||
stringName = "My,name,is,Rigoberto" | ||
listName = stringName.rsplit(",", 1) | ||
In this example, we split the string `address` by commas, but we limit the number of splits to 1. This means that the string will only be split at the first comma encountered, and the resulting list `components` will contain two elements: the street address and the rest of the address. | ||
|
||
print(listName) #Output: ["My,name,is", "Rigoberto"] | ||
``` | ||
### Splitting a String into Characters | ||
|
||
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. | ||
```python runable=true | ||
word = "Python" | ||
characters = word.split("") | ||
|
||
|
||
### `.splitlines()` Method in Python | ||
|
||
|
||
|
||
The `.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: | ||
|
||
```py | ||
stringName = "My\nname\nis\nRigoberto" | ||
listName = stringName.splitlines() | ||
|
||
print(listName) #Output: ["My","name", "is", Rigoberto"] | ||
print(characters) | ||
``` | ||
|
||
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 `.splitlines()` method**: | ||
|
||
- **True** will include the line breaks in our results as follows | ||
```py | ||
stringName = "My\nname\nis\nRigoberto" | ||
listName1 = stringName.splitlines(True) | ||
Here, we are using the `split()` method passing an empty string as an argument, this will split the string by each of the characters. Each character in the string becomes an element in the resulting list `characters`. | ||
|
||
print(listName1) #Output: ["My\n","name\n", "is\n", Rigoberto"] | ||
``` | ||
### Splitting a Multiline String into Lines | ||
|
||
- **False** will not include the line breaks in our results | ||
```py | ||
stringName = "My\nname\nis\nRigoberto" | ||
listName2 = stringName.splitlines(False) | ||
```python runable=true | ||
multiline_text = "First line\nSecond line\nThird line" | ||
lines = multiline_text.split("\n") | ||
|
||
print(listName2) #Output: ["My","name", "is", Rigoberto"] | ||
print(lines) | ||
``` | ||
|
||
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: | ||
|
||
- If we wrtie **0**, it wont include the line breaks in our results | ||
```py | ||
stringName = "My\nname\nis\nRigoberto" | ||
listName1 = stringName.splitlines(0) | ||
In this example, we split the multiline string `multiline_text` into individual lines using the newline character (`\n`) as the separator. The resulting list `lines` will contain each line of the original text as an element. | ||
|
||
print(listName1) #Output: ["My","name", "is", Rigoberto"] | ||
``` | ||
|
||
- If we write **any positive or negative numbers**, it will include the line breaks in our results | ||
```py | ||
stringName = "My\nname\nis\nRigoberto" | ||
listName2 = stringName.splitlines(1) | ||
listName3 = stringName.splitlines(2) | ||
listName4 = stringName.splitlines(100) | ||
listName5 = stringName.splitlines(-57) | ||
|
||
print(listName2) #Output: ["My\n","name\n", "is\n", Rigoberto"] | ||
print(listName3) #Output: ["My\n","name\n", "is\n", Rigoberto"] | ||
print(listName4) #Output: ["My\n","name\n", "is\n", Rigoberto"] | ||
print(listName5) #Output: ["My\n","name\n", "is\n", Rigoberto"] | ||
``` | ||
These examples demonstrate the various ways you can leverage the `split()` method in Python to manipulate strings and extract desired information. | ||
|
||
Check [4Geeks](https://4geeks.com/) to learn more and keep on the Geek Side! | ||
In this article, we've covered various methods to split a string into substrings in Python, an essential skill for effective data processing and manipulation. By exploring practical examples and best practices, you're better equipped to handle diverse string manipulation tasks in your [Python projects](https://4geeks.com/interactive-coding-tutorials?techs=python). For more insights into Python programming and data processing, and to further enhance your skills, consider signing up for free on our [Start Coding using Python](https://4geeks.com/start-coding-using-python) course at 4Geeks.com. |
Oops, something went wrong.