How to print WITHOUT New line in Python using print method?

Dung Do Tien Feb 10 2022 36

Hello,

I am a newbie in Python and I have a list:

ourPositions = ["FrontEnd", "BackEnd", "Designer", "Transfer Html", "Project Onwer"]

And I want to print the values inside the list using for-loop. So here I use of print() to display the values inside the list as shown in the example below:

ourPositions = ["FrontEnd", "BackEnd", "Designer", "Transfer Html", "Project Onwer"]
for i in ourPositions:
    print(i)

And output:

FrontEnd
BackEnd
Designer
Transfer Html
Project Onwer

You can see they break new line for each item of list. I want to print all of them in a single line and separate by a comma (,). How can I do it?

Thankyou for any suggestions.

Have 2 answer(s) found.
  • C

    Chaouch Nesrine Feb 10 2022

    I think the best way, you no need to loop with. You can use join() method to help convert a list to a string with commas separate. 

    You can see an example below:

    ourPositions = ["FrontEnd", "BackEnd", "Designer", "Transfer Html", "Project Onwer"]
    stringPos = ", ".join(ourPositions)
    print(stringPos)

    And my output:

    FrontEnd, BackEnd, Designer, Transfer Html, Project Onwer

    You can see it is so easy.

  • S

    Sree Vs Feb 10 2022

    It's so easy, you can change print(i) to print(i, end=",")

    ourPositions = ["FrontEnd", "BackEnd", "Designer", "Transfer Html", "Project Onwer"]
    for i in ourPositions:
        print(i, end=",")

    #Output

    FrontEnd,BackEnd,Designer,Transfer Html,Project Onwer,
    
Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close