Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String + Number from List
#1
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?
Reply
#2
Use string formatting.
https://python-forum.io/Thread-Basic-str...xpressions
Reply
#3
(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.
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
Reply
#4
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|
Reply
#5
(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,
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
Reply
#6
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
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 1,738 May-02-2021, 03:45 PM
Last Post: Anldra12
  cursor.execute: How to insert dynamic number in a string? stoeberhai 2 1,116 Mar-18-2021, 12:55 PM
Last Post: stoeberhai
  Regular expression: cannot find 1st number in a string Pavel_47 2 924 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Count number of occurrences of list items in list of tuples t4keheart 1 940 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  How do I add a number to every item in a list? john316 2 666 Oct-28-2020, 05:29 PM
Last Post: deanhystad
  Please support regex for version number (digits and dots) from a string Tecuma 4 1,165 Aug-17-2020, 09:59 AM
Last Post: Tecuma
  Print the number of items in a list on ubuntu terminal buttercup 2 946 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  Make an array of string number in a List polantas 5 1,443 May-27-2020, 07:18 AM
Last Post: buran
  adding elements to a list that are more than a specific number Olavv 2 990 Mar-19-2020, 06:05 PM
Last Post: Olavv
  Adding elements to a list by number Olavv 4 1,283 Mar-08-2020, 11:16 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020