a=[1,2,3]
print "the list is :%"%(a)
I want to print out one line like this: the list is:[1,2,3]
I can not make it within one line and I have to do in this way:
print " the list is :%"
print a
I am wondering whether I can print out something that combine with string formatting and a list in ONE line.
Convert a list to a string for display : If it is a list of strings we can simply join them using join() function, but if the list contains integers then convert it into string and then use join() function to join them to a string and print the string.
When you wish to print the list elements in a single line with the spaces in between, you can make use of the "*" operator for the same. Using this operator, you can print all the elements of the list in a new separate line with spaces in between every element using sep attribute such as sep=”/n” or sep=”,”.
To print multiple expressions to the same line, you can end the print statement in Python 2 with a comma ( , ). You can set the end argument to a whitespace character string to print to the same line in Python 3.
I suggest using the string format
method, as it is recommended over the old %
syntax.
print("the list is: {}".format(a))
The simplest way is what CodeHard_or_HardCode said in the comments. For Python 3 it would be:
a=[1,2,3]
print('This is a list', a)
This is a list [1, 2, 3]
Try this:
a = [1,2,3]
print("the list is: %s" % a)
At least in Python 2.7.4., this will work:
print " the list is " + str(a)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With