using print with a comma in python 3 [duplicate]

  • Last Update :
  • Techknowledgy :

This does not work in Python 3 because in Python 3, print is a function. To get the desired affect, use:

myMessage = "Hello"
for i in range(100):
   print(myMessage, end = " ")

or more concisely:

print(" ".join("Hello"
   for _ in range(100)))

You can do this with a tuple:

print( * ('Hello'
   for x in range(100)))

Or with a list (created by a list comprehension):

print( * ['Hello'
   for x in range(100)

Another way would be to use join():

print(' '.join('Hello'
   for x in range(100)))

In Python 2.x this was possible:

for x in xrange(100):
   print 'Hello',

In Python 3, simply

print( * ('Hello'
   for _ in range(5)))

To have something other than one space between each "Hello", pass the sep argument (note it's keyword-only):

print( * ('Hello'
   for _ in range(5)), sep = ', ')

gives you

Hello, Hello, Hello, Hello, Hello

Suggestion : 2

Last Updated : 27 Jun, 2022

Input: Geeks
for Geeks
Output: Geeks

Input: Python is great and Java is also great
Output: is also Java Python and great

and great Java Python is also

and great Java Python is also

Python is great and Java also

Suggestion : 3

As stated in earlier tutorials, the print() function tells Python to immediately display a given string once the command is executed. To designate a string for the print function to display, surround it in either single-quotes (' ') or double-quotes (" "). Both options are available so you can still use quotes within your string if need be. Ex: print("how are you doin' today?") ,Surrounding a string with triple double-quotes (""" """) allows you to have any combination of quotes and line breaks within a string and Python will still interpret it as a single entity. ,If the pound symbol (#) is placed before a command or any sort of string of characters, the command will appear in red and Python will ignore it during code execution. This can be used within Python to provide helpful comments to those looking at your code, or to "turn off" certain lines of code in order to test for bugs. , There are at least three ways to print Fleas, Adam, Had'em (the shortest English poem ever written apparently) in three separate lines, using one print() function. What are they? Try in IDLE shell.

>>> print('apple', 'orange', 'pear')
apple orange pear
>>> print('apple\torange')
apple orange
>>> print('It\'s raining')
It 's raining  >>>
   'It\'s raining'
# Same string specified differently
   "It's raining" >>>
"hello" >>>
print('"\\" is the backslash') # Try with "\" instead of "\\
"\" is the backslash 
>>> print("I'm hungry")
I 'm hungry  >>>
   print('I\'m hungry')
I 'm hungry  >>>
      "I'm hungry"
I 'm hungry  >>>
>>> print("Fleas\nAdam\nHad'em")
Had 'em  >>>
Had 'em  >>>
      Adam Had 'em""")
      Fleas Adam Had 'em  >>>