Python3: How to Print Out User Input String and Print It Out Separated by a Comma

Accept Commas and Spaces In User Input

As suggested in comment, just replace comma , by space character before splitting the input string.

def get_dataset():

while True:

try:
dataset = [float(_) for _ in input("\nEnter Dataset: ").replace(',', ' ').split()]
except ValueError:
print("\nInvalid Input")
continue

if len(dataset) < 2:
print("\nPlease enter at least 2 values.")
else:
return dataset

SAMPLE RUN

>>> print(get_dataset())
Enter Dataset: >? 12,3,4
[12.0, 3.0, 4.0]

One space is added in the beginning of input string

The comma is causing an extra space before the user input string. Replace the comma in the print statement with + sign.

UserInput = input('Enter a string: ')
print("\nEntered string is:\n" + UserInput)

Output:

Enter a string: I need this, but not this

Entered string is:
I need this, but not this


Related Topics



Leave a reply



Submit