Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7790823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:42:19+00:00 2026-06-01T21:42:19+00:00

In Python, I’m trying to add elements from an array into a string using

  • 0

In Python, I’m trying to add elements from an array into a string using “%s”.

However, at compile time, the size of the array, and the string i’m interpolating into, are unknown. The idea is that i’m making a madlibs type script which pulls the text from a separate file.

Current code:

from sys import argv

script, input = argv

infile = open(input)

madlibs = eval(infile.readline())
words = []

for word in madlibs:
    words.append(raw_input("Give me a %s: " % word))

print infile.read() % words

So, the first line of the input file contains the madlib questions, and the subsequent text has the story. Here is the example input file i’m using:

["noun", "verb", "verb", "noun"]
There once was a %s named Bill.
He liked to %s all the time.
But he did it too much, and his girlfriend got mad.
She then decided to %s him to get back at him.
He died. It's a sad story.
They buried him. And on his tombstone, they placed a %s.

So, in an ideal world,

print infile.read() % words 

would work, as it would just interpolate the elements in “words” into the string pulled from the file.

However, it doesn’t, and i’m out of ideas. Any help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:42:22+00:00Added an answer on June 1, 2026 at 9:42 pm

    Make sure words is a tuple:

    print(infile.read() % tuple(words))
    

    By the way, a MadLib sometimes repeats the same supplied word. It would therefore be easier to make words a dict instead of list. You could then do something like this:

    words = {
        'man' : 'Bill',
        'store' : 'bar',
        'drink' : 'beer',
        'owner' : 'bartender',
        'action' : 'drink',
        }
    
    text = '''
    {man} walks into a {store} and orders a {drink}.
    The {owner} asks {man} what he would like to {action}
    '''
    
    print(text.format(**words))
    

    which yields

    Bill walks into a bar and orders a beer.
    The bartender asks Bill what he would like to drink
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python has this wonderful way of handling string substitutions using dictionaries: >>> 'The %(site)s
Python has a json library where you can do json.loads(s) to load json into
Python project looks like this: setup.py README Application scripts hello.py shell_scripts date.sh From hello.py
Python uses the reference count method to handle object life time. So an object
Python's convention is that variables are created by first assignment, and trying to read
Python noob using 2.7 on Windows. I'm working on making a hierarchy tree view
Python newb...beware! I am trying to recursively ftp some files. In the script below,
Python Decimal doesn't support being constructed from float; it expects that you have to
Python has re.escape() if you want to match an arbitrary string literal. In my
Python has string.startswith() and string.endswith() functions which are pretty useful. What NSString methods can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.