- How to split string variables in Python
- Split Strings at Whitespaces
- Split Strings at any Specific Character
- Split Strings certain number of times from left to right
- Split string variables certain number of times from left to right at certain character
- Split string variables certain number of times from right to left at whitespaces
- Split string variables certain number of times from right to left at specific character
- Split string variables at newlines or line breaks
- Conclusion
- Related
- Recommended Python Training
- Splitlines() Function in Python
- Syntax of splitlines() in Python
- Parameters of splitlines() in Python
- Keepends
- Return Value of splitlines() in Python
- Example of splitlines() Function in Python
- What is splitlines() in Python?
- More Examples
- Example 1 : Use of Keepends Parameter
- Example 2 : Empty String Input
- Example 3: String Input With Different Line Breaks
- Example 4 : Practical Application of Splitlines Function
- Conclusion
- See Also
How to split string variables in Python
There may be many situations where we may need to split string variables. To split string variables in python we can use split() method, rsplit() method and splitlines() method with different arguments to the functions to accomplish different tasks. In this article, we will be looking at the several ways in which string variables can be split in python.
Split Strings at Whitespaces
To split string variables at each whitespace, we can use the split() function with no arguments.
The syntax for split() function is split(separator, maxsplit) where the separator specifies the character at which the string should be split. maxsplit specifies the number of times the string has to be split. By default, separator has a value of whitespace which means that if no arguments are specified, the string will be split at whitespaces . The parameter maxsplit has a default value of -1 which denotes that string should be split at all the occurrences of the separator .
When we invoke the split() method at any string without any argument, it will perform the python string split operation at every place where whitespace is present and returns a list of the substrings.
myString="Python For Beginners" print("The string is:") print(myString) myList=myString.split() print("Output List is:") print(myList)
The string is: Python For Beginners Output List is: ['Python', 'For', 'Beginners']
Split Strings at any Specific Character
To split string variables in python at any specific character, we can provide the character as the separator parameter.
For example, if we want to split the string Python_For_Beginners at places where underscore _ is present, we will do it as follows.
myString="Python_For_Beginners" print("The string is:") print(myString) myList=myString.split("_") print("Output List is:") print(myList)
The string is: Python_For_Beginners Output List is: ['Python', 'For', 'Beginners']
It can be seen in the output that the output list contains substrings of the input string which have been split at underscore character.
Split Strings certain number of times from left to right
If we want to split the input string at a certain number of places for the specified character, we can do it by specifying the number of splits to be done as maxsplit parameter in split() method. In this way, split() method will split the string the specified number of times from left to right.
The following program splits the string «Python For Beginners» only at one place where whitespace occurs, as described by the python comment in the program.
#The string will be split at only one place where whitespace occurs. myString="Python For Beginners" print("The string is:") print(myString) myList=myString.split(maxsplit=1) print("Output List is:") print(myList)
The string is: Python For Beginners Output List is: ['Python', 'For Beginners']
It can be seen in the output that the input string has been split only at the first whitespace and output list contains only two substrings.
Split string variables certain number of times from left to right at certain character
If we want to split the input string at a certain number of places for the specified character, we can do it by specifying the character at which the string has to be split as first argument and number of splits to be done as maxsplit parameter and the in split() method.
For example, if we want to split the string Python_For_Beginners at only one place from left to right where underscore _ is present, we can do it as follows.
myString="Python_For_Beginners" print("The string is:") print(myString) myList=myString.split("_",maxsplit=1) print("Output List is:") print(myList)
The string is: Python_For_Beginners Output List is: ['Python', 'For_Beginners']
Split string variables certain number of times from right to left at whitespaces
Suppose we want to split a string a certain number of times from right to left. For this we can use rsplit() method.
The syntax for rsplit() function is rsplit(separator, maxsplit) where the separator specifies the character at which the string should be split. maxsplit specifies the number of times the string has to be split. By default, separator has a value of whitespace which means that if no arguments are specified, the string will be split at whitespaces . The parameter maxsplit has a default value of -1 which denotes that string should be split at all the occurrences of the separator .
To split a string at whitespaces certain number of times from right to left, we can specify the number of times the string has to be split at maxsplit parameter.
myString="Python For Beginners" print("The string is:") print(myString) myList=myString.rsplit(maxsplit=1) print("Output List is:") print(myList)
The string is: Python or Beginners Output List is: ['Python For', 'Beginners']
In the above output, we can observe that string has been split from right to left unlike the split() method which splits the string from left to right.
Split string variables certain number of times from right to left at specific character
To split a string certain number of times from right to left at specific character, we can specify the character at which the string has to be split as first argument of rsplit() method.
For example,if we want to split the string Python_For_Beginners at only one place from right to left where underscore _ is present, we can do it as follows.
myString="Python_For_Beginners" print("The string is:") print(myString) myList=myString.rsplit("_",maxsplit=1) print("Output List is:") print(myList)
The string is: Python_For_Beginners Output List is: ['Python_For', 'Beginners']
Split string variables at newlines or line breaks
To split a string variable at line breaks, we can use splitlines() method in python. When invoked on any string, it returns a list of substrings of the original string which has been split at the line breaks to create the substrings.
myString="Python is a good language.\n I love PythonForBeginners" print("The string is:") print(myString) myList=myString.splitlines() print("Output List is:") print(myList)
The string is: Python is a good language. I love PythonForBeginners Output List is: ['Python is a good language.', ' I love PythonForBeginners']
It can be observed in the output that original string contains two lines of text and output list contains both the lines as its elements.
Conclusion
In this article, we have seen various ways to split a string variable in python by using split() , rsplit() and splitlines() method. Stay tuned for more articles.
Related
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.
Splitlines() Function in Python
Splitlines() function in python generates a list of strings obtained by splitting the given string at specified line breaks.
Syntax of splitlines() in Python
Now, since the splitlines() function is a built-in function for the string data type in Python, its syntax is similar to other string methods, as shown below:
Here, we can observe that the splitlines() function accepts an optional parameter known as keepends. Let’s understand this parameter and its use in the splitlines() method.
Parameters of splitlines() in Python
As discussed earlier, the splitlines() function accepts only one optional parameter, which is described below:
Keepends
- Type of the parameter — Boolean
- Default value — False
- Function — When the Keepends parameter is set to True, the line break statements are also included in the resulting list.
Now, let’s look at the return value of the splitlines() function. .
Return Value of splitlines() in Python
The splitlines() function returns a comma-separated list of lines extracted from a given string having line breaks. But what would happen if we give the splitlines() function an empty string or a string having no line breaks as the input?
Let’s try to understand and explore this by learning how exactly the splitlines() function is working under the hood.
Example of splitlines() Function in Python
Let’s look at an example to understand the functionality of the splitlines() function:
Here, we can notice that the splitlines() function returned a Python List that contains the sentences or lines present in the multi-line string input ( para ). .
What is splitlines() in Python?
Consider a scenario where you are given a task to find the sentence having the largest number of words from a given paragraph. For accomplishing this task, the first and foremost step is to extract all the sentences from the paragraph, i.e., we need a function to split the paragraph or multi-line string into a collection of sentences.
For this task, Python provides a special built-in function known as splitlines() that can split the multi-line string into a list of sentences, i.e., it returns a list of splitted lines.
To understand the working of the splitlines() function in Python, we need to understand the concept of line boundaries.
Line Boundaries are special statements that format text, perform line wrapping, or indicate potential line breaks. In python, these lines boundaries include:
Representation | Description |
---|---|
\n | Line Feed |
\r | Carriage Return |
\r\n | Carriage Return + Line Feed |
\v or \x0b | Line Tabulation |
\f or \x0c | Form Feed |
\x1c | File Separator |
\x1d | Group Separator |
\x1e | Record Separator |
\x85 | Next Line (C1 Control Code) |
\u2028 | Line Separator |
\u2029 | Paragraph Separator |
The splitlines() function uses these line boundaries to locate the breakpoints and then splits the string into different parts that are stored into a Python List.
More Examples
Let’s look at some examples to understand how we can use the splitlines() function.
Example 1 : Use of Keepends Parameter
In this example, we will notice the effect on the result obtained from splitlines() function when the Keepends parameter is set to True.
Here, we can observe that the newline character ( \n ) is also present in the result of the splitlines() function.
Example 2 : Empty String Input
In this example, we will notice the effect on the result obtained from the splitlines() function when we provide an empty string or a string with no line breaks as the input.
Here, we can observe that if the input string is empty, the splitlines() function returns an empty list. Whereas, If there are no line-break characters (or line boundaries) present in the string, it returns a single-line list, i.e., it returns a list having the input string as its only element.
Example 3: String Input With Different Line Breaks
In this example, let’s understand the splitting process when the input string contains different line breaks.
Here, we can observe that the splitlines() function splits the string on each specified line break.
Example 4 : Practical Application of Splitlines Function
Now, let’s look at the practical application of the splitlines() function that we already discussed at the very beginning of this article, i.e., finding the sentence having the maximum number of words.
In the above example, firstly, we extract the sentences from the input para using the splitlines() function. We further split the sentences using another built-in string function called split() . The split() function returns a list of words by splitting the string at whitespace characters. Then we can calculate the number of words in the string by simply using the len() function.
Here, we can observe that sentence four has the largest number of words (equals 10 ), which was the expected result.
Conclusion
- Splitlines() is a built-in function for strings in Python.
- It generates a list of lines from a given string.
- It splits a string according to the occurrence of different Line Boundaries.
- In the case of an empty string, it returns an empty list.
- In the absence of line breaks, it returns a unit-length list.
- It can be used to extract sentences from a paragraph.
See Also
Some similar built-in Python functions are: