Hướng dẫn dùng separate comma python

input will always return a string, even if it is a digit

If you want a number you write int(input"Enter number :))

I don't know what's the relation between your output and input, however you are looking to enter a list as an input. You can do that by

s = (input("Enter the numbers :"))
numbers = list(map(int, s.split()))

This will return a list in numbers. You enter the numbers with a space in between not a comma

E.g

Enter the numbers :100 97 84
Ouput>>>[100, 97, 84]

If you want them without the brackets and separated by a comma, you can use print(*numbers, sep=',') will give

Enter the numbers :100 97 84
Output>>>100,97,84

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given an input string that is comma-separated instead of space. The task is to store this input string in a list or variables. This can be achieved in Python using two ways:

    • Using List comprehension and split()
    • Using map() and split()

    Method 1: Using List comprehension and split()

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

    Example:

    a, b = [int(x) for x in input("Enter two values\n").split(', ')]

    print("\nThe value of a is {} and b is {}".format(a, b))

    a, b, c = [int(x) for x in input("Enter three values\n").split(', ')]

    print("\nThe value of a is {}, b is {} and c is {}".format(a, b, c))

    L = [int(x) for x in input("Enter multiple values\n").split(', ')]

    print("\nThe values of input are", L) 

    Output:

    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    

    Method 2: Using map() and split()

    map() function returns a list of the results after applying the given function to each item of a given iterable (list, tuple etc.)

    a, b = map(int, input("Enter two values\n").split(', '))

    print("\nThe value of a is {} and b is {}".format(a, b))

    a, b, c = map(int, input("Enter three values\n").split(', '))

    print("\nThe value of a is {}, b is {} and c is {}".format(a, b, c))

    L = list(map(int, input("Enter multiple values\n").split(', ')))

    print("\nThe values of input are", L)

    Output:

    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 2, 3, 4
    
    The values of input are [1, 2, 3, 4]
    

    How do you extract Comma Separated Values in Python?

    Python String split() Method Python split() method splits the string into a comma separated list. It separates string based on the separator delimiter. This method takes two parameters and both are optional. It is described below.

    How do you use comma separated input?

    How to input a comma separated string?.

    Get the string to be taken as input in stringstream..

    Take each character of the string one by one from the stream..

    Check if this character is a comma (', ')..

    If yes, then ignore that character..

    Else, Insert this character in the vector which stores the words..

    How do you split inputs in Python?

    Using split() method : This function helps in getting a multiple inputs from user. It breaks the given input by the specified separator. If a 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 use it in taking multiple input.