for i in range(10):
print(i, sep = ',', end = '')
It should be 0,1,2,3,4,5,6,7,8,9
,but the truth is The
sep
does not work. Thanks!
The optional sep
argument is used to define the separator between comma separated argument values that are fed to the objects
parameter of print
. objects
is a variable argument parameter, which means it can take any number of arguments (or an unpacked iterable).
From the docs :
print(*objects, sep=' ', end='\\n', file=sys.stdout, flush=False)
Print objects to the text stream file, separated by sep and followed by end. sep, end and file, if present, must be given as keyword arguments.
All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.
A better way of writing your code would be the following:
print(*range(10), sep=',', end='')
This uses the *
operator to unpack the iterable and feed each of its elements in as arguments to the print
function.
It is equivalent to:
print(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, sep=',', end='')
In this loop, i
is always a one digit integer and this can't be seperated.
It would be like this:
print("1", sep=',', end='')
print("2", sep=',', end='')
print("3", sep=',', end='')
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.