La domanda chiede: "Come può essere fatto in Python 3?"
Utilizzare questo costrutto con Python 3.x:
for item in [1,2,3,4]:
print(item, " ", end="")
Questo genererà:
1 2 3 4
Vedere questo Python doc per ulteriori informazioni:
Old: print x, # Trailing comma suppresses newline
New: print(x, end=" ") # Appends a space instead of a newline
-
parte:
in aggiunta, la funzione print()
offre anche il parametro sep
che permette un specificare in che modo i singoli elementi da stampare dovrebbero essere separati. Ad esempio,
In [21]: print('this','is', 'a', 'test') # default single space between items
this is a test
In [22]: print('this','is', 'a', 'test', sep="") # no spaces between items
thisisatest
In [22]: print('this','is', 'a', 'test', sep="--*--") # user specified separation
this--*--is--*--a--*--test
Si può solo fare 'di stampa ('' .join ([str (i) for i in [1, 2, 3, 4]]))' ' –
di stampa (* [1, 2, 3, 4]) 'funziona per il caso comune di stampare una sequenza separata dallo spazio –