How do you combine two inputs in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The 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

    Using split[] method : 
    This function helps in getting multiple inputs from users. It breaks the given input by the specified separator. If a separator is not provided then any white space is a separator. Generally, users use a split[] method to split a Python string but one can use it in taking multiple inputs.

    Syntax : 

    input[].split[separator, maxsplit]

    Example : 

    Python3

    x, y = input["Enter two values: "].split[]

    print["Number of boys: ", x]

    print["Number of girls: ", y]

    print[]

    x, y, z = input["Enter three values: "].split[]

    print["Total number of students: ", x]

    print["Number of boys is : ", y]

    print["Number of girls is : ", z]

    print[]

    a, b = input["Enter two values: "].split[]

    print["First number is {} and second number is {}".format[a, b]]

    print[]

    x = list[map[int, input["Enter multiple values: "].split[]]]

    print["List of students: ", x]

    Output: 
     

    Using List comprehension : 
    List comprehension is an elegant way to define and create list in Python. We can create lists just like mathematical statements in one line only. It is also used in getting multiple inputs from a user. 

    Example: 

    Python3

    x, y = [int[x] for x in input["Enter two values: "].split[]]

    print["First Number is: ", x]

    print["Second Number is: ", y]

    print[]

    x, y, z = [int[x] for x in input["Enter three values: "].split[]]

    print["First Number is: ", x]

    print["Second Number is: ", y]

    print["Third Number is: ", z]

    print[]

    x, y = [int[x] for x in input["Enter two values: "].split[]]

    print["First number is {} and second number is {}".format[x, y]]

    print[]

    x = [int[x] for x in input["Enter multiple values: "].split[]]

    print["Number of list is: ", x] 

    Output : 
     

    Note: The above examples take input separated by spaces. In case we wish to take input separated by comma [, ], we can use the following: 

    Python3

    x = [int[x] for x in input["Enter multiple value: "].split[","]]

    print["Number of list is: ", x] 

    Please see //ide.geeksforgeeks.org/BHf0Cxr4mx for a sample run.
     


    How do you join two inputs in Python?

    Example -.
    # taking two inputs at a time..
    a, b, c = input["Enter three values: "].split[].
    print["Enter Your First Name: ", a].
    print["Enter Your Last Name: ", b].
    print["Enter Your Class: ", c].
    print[].
    # taking three inputs at a time..
    x, y, z = input["Enter three values: "].split[].

    Can you have two inputs in Python?

    With the help of the split [] function, developers can easily collect multiple inputs in Python from the user and assign all the inputs to the respective variables.

    How do you concatenate variables in Python?

    Use the + operator.
    str1="Hello".
    str2="World".
    print ["String 1:",str1].
    print ["String 2:",str2].
    str=str1+str2..
    print["Concatenated two different strings:",str].

    Chủ Đề