Posts: 5
Threads: 4
Joined: Aug 2019
Aug-25-2019, 10:38 AM
(This post was last modified: Aug-25-2019, 10:46 AM by Yoriz.)
I have a string and a list with numbers.
1 2 3 |
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?
Posts: 2,168
Threads: 35
Joined: Sep 2016
Posts: 212
Threads: 25
Joined: Aug 2019
Aug-25-2019, 11:36 AM
(This post was last modified: Aug-25-2019, 11:56 AM by newbieAuggie2019.)
(Aug-25-2019, 10:38 AM)BollerwagenIng Wrote: I have a string and a list with numbers.
1 2 3 |
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:
1 2 3 4 5 6 |
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
already provides the space.
If you don't want to change myString, then you could also do something like:
1 2 3 4 5 6 7 |
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,
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
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.
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Posts: 7,319
Threads: 123
Joined: Sep 2016
Aug-25-2019, 01:28 PM
(This post was last modified: Aug-25-2019, 01:28 PM by snippsat.)
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.
1 2 3 4 |
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.
1 2 3 4 |
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|
Posts: 212
Threads: 25
Joined: Aug 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.
1 2 3 4 |
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.
1 2 3 4 |
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,
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Posts: 1,950
Threads: 8
Joined: Jun 2018
If oneliners are your thingy then one can also do:
1 2 3 4 5 6 7 |
>>> 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
|
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
|