anderssinho
anderssinho

Reputation: 298

Two list with string. Pick random from each list and fuse them together with %s in Python

I have 2 list defined:

    lunchQuote=['ska vi ta %s?','ska vi dra ned till %s?','jag tänkte käka på %s, ska du med?','På %s är det mysigt, ska vi ta där?']

lunchBTH=['thairestaurangen vid korsningen','det är lite mysigt i fiket jämte demolabbet','Indiska','Pappa curry','boden uppe på parkeringen'
,'Bergåsa kebab','Pasterian','Villa Oscar','Eat here','Bistro J']

And as you can see in the first list I wanna use the %s to bind to of the strings of each list together to a new string that I can send back to the program.

One example of a successful new string could be:

ska vi ta thairestaurangen vid korsningen?

The problem is I don't really understand how it works with %s and what I have to do before I can use it. I worked with .format before but then just on a regular string, not strings from two different lists.

I have started with saving the randomly selected choices and save them in a variable:

 lunchQuote = random.choice(lunchQuote)
        BTH = random.choice(lunchBTH)

What should I do next?

Upvotes: 0

Views: 76

Answers (1)

Francesco Nazzaro
Francesco Nazzaro

Reputation: 2916

The best way is using format method, defining lunchQuote as follow

import random

lunchQuote=['ska vi ta {}?', 'ska vi dra ned till {}?', 'jag tänkte käka på {}, ska du med?', 'På {} är det mysigt, ska vi ta där?']

lunchBTH=['thairestaurangen vid korsningen', 'det är lite mysigt i fiket jämte demolabbet', 'Indiska', 'Pappa curry', 'boden uppe på parkeringen', 'Bergåsa kebab', 'Pasterian','Villa Oscar', 'Eat here', 'Bistro J']

lunchQuote = random.choice(lunchQuote)
BTH = random.choice(lunchBTH)

print(lunchQuote.format(BTH))

You can use also %s as follow:

import random

lunchQuote=['ska vi ta %s?', 'ska vi dra ned till %s?', 'jag tänkte käka på %s, ska du med?', 'På %s är det mysigt, ska vi ta där?']

lunchBTH=['thairestaurangen vid korsningen', 'det är lite mysigt i fiket jämte demolabbet', 'Indiska', 'Pappa curry', 'boden uppe på parkeringen', 'Bergåsa kebab', 'Pasterian','Villa Oscar', 'Eat here', 'Bistro J']

lunchQuote = random.choice(lunchQuote)
BTH = random.choice(lunchBTH)

print(lunchQuote % BTH)

Upvotes: 1

Related Questions