(Aug-25-2019, 10:38 AM)BollerwagenIng Wrote: I have a string and a list with numbers.
myString = ("go ")
myList = [1, 2, 3, 4]
And I want that output:
Output:
go 1
go 2
go 3
go 4
how can I connect them?
Hi!
I think you could do something like:
myString = ("go")
myList = [1, 2, 3, 4]
for x in myList:
print(myString, x)
This little program gives the following output:
Output:
go 1
go 2
go 3
go 4
Notice that I have eliminated the last space in myString, as the comma (,) inside the print command
print(myString, x)
already provides the space.
If you don't want to change myString, then you could also do something like:
myString = ("go ")
myList = [1, 2, 3, 4]
for x in myList:
x = str(x)
print(myString + x)
with the following output:
Output:
go 1
go 2
go 3
go 4
The differences in the 2 little programs are that in the first one with the comma in the print command,
print(myString, x)
you can print a string beside an integer, while in the second program, you cannot concatenate (join), a string with an integer, so I have cast (transformed) the integers into strings, to be able to concatenate them, with the command
x = str(x)
and then using the operator '+' to concatenate them. Keep in mind, though, that with the operator '+', the strings are printed without spaces between them.
I hope it helps.