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
Question Extracting Version Number from a String britesc 2 1,030 May-31-2023, 10:20 AM
Last Post: britesc
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,012 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,757 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,196 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Divide a number by numbers in a list. Wallen 7 7,925 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  When did the number got included in the list? Frankduc 14 2,980 Feb-03-2022, 03:47 PM
Last Post: Frankduc
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 5,105 May-02-2021, 03:45 PM
Last Post: Anldra12
  cursor.execute: How to insert dynamic number in a string? stoeberhai 2 3,447 Mar-18-2021, 12:55 PM
Last Post: stoeberhai
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,364 Jan-15-2021, 04:39 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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