Home Python Python String split() methods

Python String split() methods

Split() provides a list of all the words in the string using str as the separator (splits on all whitespace if left unspecified), with the number of splits limited to num. It essentially returns a list of strings after splitting a string at the provided separator.

Syntax

The split() method’s syntax is as follows:

str.split(str="", num=string.count(str))

Note that the split() method can take up to a maximum of 2 parameters.

Parameter function

str – It can be any delimiter; by default, it is space.

Num – Maximum number of splits (optional). There is no limit to the number of splits if no limit is specified. When maxsplit is specified, the list will have the number of elements specified plus one. The default value is -1, which refers to “all occurrences .”Overall, it is the given number of lines less than one.

Value of the return -This procedure returns a list of strings.

Python String split() methods

Example 1: The split() technique

The split() technique is demonstrated in the following example.

str = "String1-abcdef \nString2-abc \nString4-abcd";
print(str.split( ))
print(str.split(' ', 1 ))
The split() technique
The split() technique

Example 2: split the text from space

text = 'Codeunderscored is highly specialized in Python'
print(text.split(' '))
split the text from space
split the text from space

Example 3: Hash character as the separator

fruits = "mango#lemon#bananas#apples"

resultant_fruits = fruits.split("#")
  
print(resultant_fruits)
Hash character as the separator
Hash character as the separator

Example 4: How split() works in Python

text= 'Doing the best is always the solution.'

# splits at space
print(text.split())
  
laptops = 'dell, IBM, Apple'
  
# splits at ','
print(laptops.split(', '))

# Splits at ':'
print(laptops.split(':'))
  

split() in action

Example 5 : How split() works when maxsplit is specified?

laptops = 'Apple, IBM, Lenovo, HP'
  
# maxsplit: 2
print(laptops.split(', ', 2))
  
# maxsplit: 1
print(laptops.split(', ', 1))
  
# maxsplit: 5
  
print(laptops.split(', ', 5))
  
# maxsplit: 0
print(laptops.split(', ', 0))
split() when maxsplit is specified
split() when maxsplit is specified

Example 5: How setting maxsplit to 1 returns two elements

fruits = "mango#lemon#bananas#apples"
  
resultant_fruits = fruits.split("#", 1)
  
print(resultant_fruits)
  
setting maxsplit to 1 returns two elements
setting maxsplit to 1 returns two elements

Example 6: Demonstrating the split() method when maxsplit is specified

code_info = 'code, underscored, for , Python, is, exceptional'

# maxsplit: 0
print(code_info.split(', ', 0))

# maxsplit: 4
print(code_info.split(', ', 4))

# maxsplit: 1
print(code_info.split(', ', 1))
split() method when maxsplit is specified
split() method when maxsplit is specified


Conclusion

You’ve learned the following in this tutorial:

  • <string>.split(sep, maxsplit) separates the string based on the type of separation provided and maxsplit.
  • You may need to split a string into substrings while working with strings in Python. As a result, the split() method is useful when splitting a string into substrings. In addition, it has two optional parameters that comprise the separator and the maxsplit.

You may also like

Leave a Comment