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,486
  • 4
  • 17
  • 25
Maverick
  • 49
  • 1
  • 6

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,822
  • 14
  • 22
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!

erik258
  • 14,701
  • 2
  • 25
  • 31
Torin M.
  • 523
  • 1
  • 7
  • 26
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
  • 5,219
  • 5
  • 21
  • 44