PYTHON3: How to print out user input string and print it out separated by a comma?

how to take comma separated input in python 3
how to print a comma in python
python print separated by comma
python split string by comma into list
python convert comma separated string to list of integers
how to print comma separated values in python
how to add comma in string python
python separate list by comma

My current code is:

def my_input(): 
    my_string = input("Important ions are: ")
    for i in my_string:
        print(my_string, sep=",")

my_input()

So I am trying to write a program, whereby the user is prompted "Important elements are:" and is expected to input:

Na K Ca Mg Mn(all on one line separated by single spaces):

Then, I want my code to print out the following:

Important ions are: Na, K, Ca, Mg and Mn

Instead, with my current code, I am getting the following output:

Na K Ca Mg Mn
Na K Ca Mg Mn
Na K Ca Mg Mn
Na K Ca Mg Mn
Na K Ca Mg Mn
Na K Ca Mg Mn
Na K Ca Mg Mn
Na K Ca Mg Mn
Na K Ca Mg Mn
Na K Ca Mg Mn
Na K Ca Mg Mn
Na K Ca Mg Mn
Na K Ca Mg Mn

I am confused as to why/how this is the output printed that many times. I think I get why it is not being separated by a comma, is it because it is treating the input as 1 string? And I guess in order to print out my desired output, I'd have to do like

print("Important ions are" + my_string)

But I know that cannot be correct and then how could I get to print out the input, separated by commas and add an "and" in between as the desired output displayed above??

First, your guess is right, you have to add the string "Important ions are" when you print your output.

Besides, you used 'my_string' (all ions input) in your for-loop, that's why you get 'Na K Ca Mg Mn' printed for multiple times.

What you need will look like this

def my_input(): 
    my_string = input("Important ions are: ")
    ions = my_string.split(' ')
    print("Important ions are: ", end='')
    for j, i in enumerate(ions):
        if j == 0:
            print(i, end='')
        if j == (len(ions) - 1):
            print(' and ' + i)
        else:
            print(', ' + i, end='')

my_input()

Output:

# >>> my_input()
# Important ions are: Na K Ca Mg Mn
# Important ions are: Na, Na, K, Ca, Mg and Mn

Simple Input and Output in Python, How do you use comma separated input in Python 3? Consider the following problem: Prompt the user for two numbers, and then print out a sentence stating the sum. For instance if the user entered 2 and 3, you would print ‘The sum of 2 and 3 is 5.’ You might imagine a solution like the example file addition1.py, shown below. There is a problem. Can you figure it out before you try running it?

You could try this:

import re
def my_input(): 
    my_string = input("Important ions are: ").split()   # storing the input as elements of a list
    w = " ".join(my_string)                                     # joining all the elements in the list  
    print("Important ions are: ", re.sub(" ",", ",w[0:-2]) +'and '+ my_string[-1])  # appending the last element after inserting ', ' and 'and'

my_input()

This should give you the output exactly the way you wanted. The reason why your code prints the input repeatedly is because of this line:

 print(my_string, sep=",")

It just prints the whole input as many times as there elements in the input. Also, sep= "," won't work in loops.

Python program to input a comma separated string, How do you split a line with a comma in Python? This complete guide on Python input and output lets you know how to get input from the user, files, and display output on the screen, console or write it into the file. Learn how to take input from a user and system In Python. Accept an integer, float, character and string input from a user. Convert the user input to a different data type.

Python, python3 out1.py Hi, this is a Python program where user is the current user's username, host is the hostname up until the first '. When you separate the values by commas, the print function automatically When you print out a literal string, the output is literally everything that is in between the quotes. Simple Input and Output in Python. In any programming language, including Python, output is always simpler than input. So, we will start off looking at simple output. Simple output in Python. Simple output is one of the first things that you would learn when learning a new programming language.

Basic Input, Output, and String Formatting in Python – Real Python, Given an input string that is comma-separated instead of space. The task is to store print ( "\nThe value of a is {}, b is {} and c is {}" . format (a, b, c)). # Taking  Note: print() was a major addition to Python 3, in which it replaced the old print statement available in Python 2. There were a number of good reasons for that, as you’ll see shortly. Although this tutorial focuses on Python 3, it does show the old way of printing in Python for reference.

Input, print and numbers - Learn Python 3, Here, we will input comma separate elements and convert into a list of integers. Submitted by IncludeHelp, on August 08, 2018 ("Enter comma separated integers: ")) print "Input string: ", str # conver to the list list = str.split (",") print "list: ", list  These methods do not write anything, they just return a new string. If the input string is too long, they don’t truncate it, but return it unchanged; this will mess up your column lay-out but that’s usually better than the alternative, which would be lying about a value.

Input and Output Tutorials & Notes | Python, Take user input from the keyboard with the built-in function input() Sheet and learn the basics of Python 3, like working with data types, Line 8 converts n to an integer so the print() statement on line 10 To display objects to the console, pass them as a comma-separated list of argument to print() . Home / Python Tutorials / Python 3 print function: 7 examples with strings, int , list, range etc. The print function in Python is used to display the output of variables: string, lists, tuples, range etc. Before version 3.x of Python, the print was taken as a statement. However, from Python 3.x, the print acts as a function.

Comments
  • And I think this is the one to go for.
  • Yes, but how to add the "and" before the last one?