split() method
- Split a string into a list where each word is a list item:
- It breaks the given input by the specified separator.
- If separator is not provided then any white space is a separator.
- 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)
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())
Run the above code and your output is look like
['my','name','is','abc']
['hii','welcome']
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())
when you run the above code and your output look like
['my','name','is','abc']
['my name','is','abc and','your name','is']
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))
['Milk', 'Chicken', 'Bread, Butter']
['Milk', 'Chicken, Bread, Butter']
['Milk, Chicken, Bread, Butter']
['Milk', 'Chicken', 'Bread', 'Butter']
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.
- Using split() method
- 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)
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
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))
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>
Thank you
Top comments (2)
f-strings are your friend
what you want to tell/ask is not clear so please make it clear.