1

In Python3, I'd like to write a blanket message that goes to all items in the list (names of dinner guests, in this case) without having to print each message individually.

For example, if the list is:

guest_list = ['John', 'Joe', 'Jack'] 

I want it to print this line below using each person's name without having to individually print the message 3 times:

print("Hello, " + *name of guest from the list above here* + "! We have found a bigger table!")

Desired Result:

Hello, John! We have found a bigger table! 
Hello, Joe! We have found a bigger table! 
Hello, Jack! We have found a bigger table! 

Is this possible? If so, how? Thanks to any help offered!

Gabriel M
  • 1,448
  • 4
  • 16
  • 25
Maverick
  • 49
  • 1
  • 5

3 Answers3

2

If you want to do only one print:

guest_list = ['John', 'Joe', 'Jack']
result_str = ['Hello {}! We have found a bigger table!'.format(guest) for guest in guest_list]
print(result_str.join('\n'))
Aurora Wang
  • 1,542
  • 13
  • 21
1

You can use a simple for loop:

guest_list = ['John', 'Joe', 'Jack']
for x in guest_list:
    print("Hello, " + x + "! We have found a bigger table!")

Hello, John! We have found a bigger table!

Hello, Joe! We have found a bigger table!

Hello, Jack! We have found a bigger table!

Daniel Farrell
  • 11,706
  • 2
  • 25
  • 27
Torin M.
  • 441
  • 1
  • 7
  • 21
1

you can do the following:

for x in guest_list:
    print("Hello, %s! We have found a bigger table!" %x)

Where %s allows you to insert a string format which is replaced by the variable I am passing to it.

d_kennetz
  • 4,918
  • 5
  • 20
  • 44