I would like to assign print output to some string variable
print "Cell:", nei, "from the neighbour list of the Cell:",cell,"does not exist"
Could you please advice?
I would like to assign print output to some string variable
print "Cell:", nei, "from the neighbour list of the Cell:",cell,"does not exist"
Could you please advice?
Use simple string concatenation:
variable = "Cell: " + str(nei) + " from the neighbour list of the Cell: " + str(cell) + " does not exist"
or string formatting:
variable = "Cell: {0} from the neighbour list of the Cell: {1} does not exist".format(nei, cell)
It's not the way Python works. If you do NOT have enough reason, simpliy use =
.
But if you insist, you may do like this.(Again, it's very unnecessary)
def printf(*args):
together = ''.join(map(str, args)) # avoid the arg is not str
print together
return together
x = printf("Cell:", nei, "from the neighbour list of the Cell:",cell,"does not exist")
If you are only try to join things into a string, there are many ways you can do:
x = ''.join(("Cell:", str(nei), "from the neighbour list of the Cell:",str(cell),"does not exist"))
x = "Cell:%s from the neighbour list of the Cell:%s"%(nei, cell)
x = "Cell:{} from the neighbour list of the Cell:{}".format(nei, cell)
x = "Cell:{key_nei} from the neighbour list of the Cell:{key_cell}".format({'key_nei':nei, 'key_cell':cell})
have a look at python's string formating and python's str