How to Take space-separated input in Python

In this post, we are going to learn how to Take space-separated input in Python with examples. The Python built-in input() function is used to prompt the user for input and the split() function to split the input with whitespace.

1. Take space-separated input in Python


To read a string input from the user in Python, the built-in input() function is used. The input() function always takes input as a string. The input() method takes a prompt as a parameter. A prompt is a string message that is displayed before input and control is given to the user.

  • In this Python program, We have used the input() function to prompt a message and take string input, and stored the input string into two variables(Str, str2).
  • Split() : To split the string input with space.We can use any whitespace( space,comma(,),colon(:) ) etc.
Str, str2 = [(ele) for ele in input("Enter two String Values: ").split()]
print("You have enter First string value: ", Str)
print("You have enter Second string: ", str2)

Output

Enter two String Values: Welcome Devenum
You have enter First string value:  Welcome
You have enter Second string:  Devenum

Most Popular Related Post


2. Take Mutiple space-separated inputs in Python


In this Python program, We are using the map(), input(), split() Python function to take space-separated input as list

  • input: To display and message and take input User.
  • split(): Split the input string and return a list of strings.
  • map(): The map() is used to apply a function to each input element of the list.
  • List: to convert the input value into the list.
value = list(map(str,input("Enter mutiple String input :").split()))
print('You have entered :',value)

Output

Enter mutiple String input :Welcome to devenum 
You have entered : ['Welcome', 'to', 'devenum']

3. For Loop to Take space-separated input in Python


In this Python program, we are using the input() function to take space-separated and split it using the split() function by default it splits the string by delimiter comma(,), iterating over splitting input using for loop and printing the result.

Input_val = input("Enter mutiple String input:") 
# input separated by spaces
for val in Input_val.split():
    print(val)

Output

Enter mutiple String input:Welcome to devenum 
Welcome
to
devenum

4. Take space-separated input with delimiter in Python


Sometimes we have to separate input strings with a special delimiter so that we will understand how to split an input string with a special delimiter of our choice. In this example, using the input() function to take input from the user and the split(,) function to split input string with delimiter comma(,) to iterate over input string value for loop is used and finally printing each splitting value.

#Python3 Take space-separated input in Python seperated by delimiter

DELIMITER = ','
Input_val = input("Enter mutiple String input:") 
for val in Input_val.split(DELIMITER):
    print(val)

Output

Enter mutiple String input:Welcome,to,Devenum
Welcome
to
Devenum

5. Take a space-separated range of input values in Python


Sometimes it needs to take a fixed range of input from the user. In this python program, First, use the input() function to take input count of the input the user want to enter.We have used these below functions to split and convert the input strings into the list. Let us understand with the below examples.

  • input(): To display and message and take input User.
  • split(): Split the input string and return a list of strings.
  • map(): The map() is used to apply a function to each input element of the list.
  • :numcount : Count of the input string
#Python3 Take space-separated input in Python range of values

numcount = int(input("Enter number of elements : "))  
  
list = list(map(str,input("\nPlease Enter Strin value  : ").strip().split()))[:numcount]
   
print("\nyou have enetered : ", list)

Output

Enter number of elements : 5

Please Enter Strin value  : Hi Welcome to Dev Enum 

you have enetered :  ['Hi', 'Welcome', 'to', 'Dev', 'Enum']

Summary

In this post we have learned how to Take space-separated input in Python using input(),split(),map() function with examples.