Python Forum
String + Number from List - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: String + Number from List (/thread-20679.html)



String + Number from List - BollerwagenIng - Aug-25-2019

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?


RE: String + Number from List - Yoriz - Aug-25-2019

Use string formatting.
https://python-forum.io/Thread-Basic-string-format-and-string-expressions


RE: String + Number from List - newbieAuggie2019 - Aug-25-2019

(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.


RE: String + Number from List - snippsat - Aug-25-2019

Your expatiation is okay @newbieAuggie2019,but there is better way to get the string output.
Convert str(x) and +,is maybe okay in this small scale,but can become ugly very soon if string is longer.
String formatting as linked be @Yoriz,is a nicer way to do it.
my_string = ("go")
my_list = [1, 2, 3, 4]
for item in my_list:
    print(f'{myString} {item}')
Output:
go 1 go 2 go 3 go 4
Also a lot of powers in this way if need an other look for the output.
my_string = ("go")
my_list = [1, 2, 3, 4]
for item in my_list:
    print(f'|{myString} -->{item:3}|')
Output:
|go --> 1| |go --> 2| |go --> 3| |go --> 4|



RE: String + Number from List - newbieAuggie2019 - Aug-25-2019

(Aug-25-2019, 01:28 PM)snippsat Wrote: Your expatiation is okay @newbieAuggie2019,but there is better way to get the string output.
Convert str(x) and +,is maybe okay in this small scale,but can become ugly very soon if string is longer.
String formatting as linked be @Yoriz,is a nicer way to do it.
my_string = ("go")
my_list = [1, 2, 3, 4]
for item in my_list:
    print(f'{myString} {item}')
Output:
go 1 go 2 go 3 go 4
Also a lot of powers in this way if need an other look for the output.
my_string = ("go")
my_list = [1, 2, 3, 4]
for item in my_list:
    print(f'|{myString} -->{item:3}|')
Output:
|go --> 1| |go --> 2| |go --> 3| |go --> 4|

Wow, thank you!

I'm still a newbie, so my knowledge is almost zero, but I'm learning a bit, mostly by imitating code I see, that I can understand and use or tweak.

Thank you for making me aware of more ways to format and use strings. I had already seen and tweaked some in a few posts here about splitting email addresses.

All the best,


RE: String + Number from List - perfringo - Aug-25-2019

If oneliners are your thingy then one can also do:

>>> my_string = ('go')
>>> my_list = [1, 2, 3, 4]
>>> print('\n'.join([f'{my_string} {item}' for item in my_list]))
go 1
go 2
go 3
go 4