DEV Community

Mohit Raj
Mohit Raj

Posted on

3.5 split() method in Python

split() method

  1. Split a string into a list where each word is a list item:
  2. It breaks the given input by the specified separator.
  3. If separator is not provided then any white space is a separator.
  4. Generally, user use a split() method to split a Python string but one can used it in taking multiple input.

Syntax :

     input().split(separator, maxsplit)
Enter fullscreen mode Exit fullscreen mode

a) seperator (optional): separator (optional)- The is a delimiter. The string splits at the specified separator.If the separator is not specified, any whitespace (space, newline etc.) string is a separator.
b) maxsplit (optional): The maxsplit defines the maximum number of splits.
The default value of maxsplit is -1, meaning, no limit on the number of splits.

Return Value from split():
The split() breaks the string at the separator and returns a list of strings.

Example 1: when no seperator is specified-

x='my name is abc'
print(x.split())

y='hii welcome'
print(y.split())
Enter fullscreen mode Exit fullscreen mode

Run the above code and your output is look like

['my','name','is','abc']
['hii','welcome']
Enter fullscreen mode Exit fullscreen mode

Example 2: when there is a seperator specified-

x='my,name,is,abc'
print(x.split())

y='my name,is,abc and,your name,is'
print(y.split())
Enter fullscreen mode Exit fullscreen mode

when you run the above code and your output look like

['my','name','is','abc']
['my name','is','abc and','your name','is']
Enter fullscreen mode Exit fullscreen mode

Example 3: when maxsplit is specified

grocery = 'Milk, Chicken, Bread, Butter'
print(grocery.split(', ', 2))
print(grocery.split(', ', 1))
print(grocery.split(', ', 0))
print(grocery.split(', ', 5))
Enter fullscreen mode Exit fullscreen mode
['Milk', 'Chicken', 'Bread, Butter']
['Milk', 'Chicken, Bread, Butter']
['Milk, Chicken, Bread, Butter']
['Milk', 'Chicken', 'Bread', 'Butter']
Enter fullscreen mode Exit fullscreen mode
Multiple input using split() method

Developer often wants a user to enter multiple values or inputs in one line. In C++/C user can take multiple inputs in one line using scanf but in Python user can take multiple values or inputs in one line by two methods.

  1. Using split() method
  2. Using List comprehension (discussed later)

a>. This function helps in getting a multiple inputs from user .
b>. It breaks the given input by the specified separator.
c>. If separator is not provided then any white space is a separator.
d>. Generally, user use a split() method to split a Python string but one can used it in taking multiple input

Examples

x, y = input("Enter two value: ").split()   # here seperator is space
print("first value is :",x) 
print("second value is :",y)

print()       # to break line

x, y = input("Enter a two value: ").split(",")   # here seperator is comma
print("first value is :",x) 
print("second value is :",y)
Enter fullscreen mode Exit fullscreen mode

when you run the above code output is look like this

Enter two value:5 10
first value is :5
second value is :10

Enter two value:5,10
first value is :5
second value is :10
Enter fullscreen mode Exit fullscreen mode

We can also take a list as an input as because split() method return a list as output.

Example

x=input("Enter the elements of the list by comma seperated :").split(",")
print(x)
print(type(x))
Enter fullscreen mode Exit fullscreen mode

output looks like

Enter the elements of the list by comma seperated : a,b,c,d,e,123,name
['a','b','c','d','e','123','name']
<class list>
Enter fullscreen mode Exit fullscreen mode

Thank you

Top comments (2)

Collapse
 
iceorfiresite profile image
Ice or Fire

f-strings are your friend

Collapse
 
mohit355 profile image
Mohit Raj

what you want to tell/ask is not clear so please make it clear.