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.

Example

myString="Python For Beginners"
print("The string is:")
print(myString)
myList=myString.split()
print("Output List is:")
print(myList)

Output:

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)

Output:

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)

Output:

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)

Output:

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)

Output:

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)

Output:

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.

Example:

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)

Output:

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.

Leave a Reply

Your email address will not be published. Required fields are marked *