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 8720979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:08:50+00:00 2026-06-13T07:08:50+00:00

In python, is it possible to have a reoccurring string-formatting operation take place when

  • 0

In python, is it possible to have a reoccurring string-formatting operation take place when an item in a list is accessed?

For example:

>>>from random import randint
>>>a = ["The random number is: {0}".format(randint(0,10))]
>>>print a[0]
The random number is: 3
>>>print a[0]
The random number is: 3

Obviously it’s obtaining a random integer, formatting the string and saving it in the list when the list is first defined. Performance hit aside, I’d like to know if it is possible to override this behavior.

I know if I were to see this question I would respond with something like “you’re doing it wrong” and would provide something similar to the below answer…

>>>a = ["The random number is: {0}"]
>>>print a[0].format(randint(0,10))

But lets assume that’s not a solution for this question. I’d really like for the formatting to be defined and take place in the list itself (if possible).

Another example:

a = ["The some sweet string: {0}".format(someFunction),
     "Another {0} different string {1}".format(someFunctionTwo, someFunctionThree)]

Where someFunction* provides a “random” result upon each call.

I know its a bit of a stretch and I may have to rely on the methods provided already ( thanks for your feedback ) but, I figured I’d give it a shot.

Thanks again.

  • 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-13T07:08:50+00:00Added an answer on June 13, 2026 at 7:08 am

    You can create a class and override __str__:

    >>> from random import randint
    >>> class Foo(object):
    ...     def __str__(self):
    ...        return "The random number is: {0}".format(randint(0,10))
    ... 
    >>> a = [Foo()]
    >>> print a[0]
    The random number is: 8
    >>> print a[0]
    The random number is: 10
    >>> print a[0]
    The random number is: 5 
    

    But you’re right, my first inclination is to say that you’re probably doing it wrong…


    Here’s another idea — keep your lists with format strings in them:

    a = ["The some sweet string: {func1}",
         "Another {func2} different string {func3}"]
    
    for item in a:
       print item.format(func1=func1(),func2=func2(),func3=func3())
    

    Obviously this isn’t efficient (as you call functions when you don’t necessarily need them …), but it could work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to have a list be evaluated lazily in Python? For example
Possible Duplicate: Python, compute list difference I have two lists For example: A =
Possible Duplicate: Calling Python from JavaScript I have a test.py and test.js . I
Possible Duplicate: True random number generator I have worked with random functions in python,ruby,
For example, if I have this code: subprocess.call(['gnome-terminal']) Is it possible to have python
Possible Duplicate: Python: simple list merging based on intersections I have a multiple list:
Possible Duplicate: python dict.add_by_value(dict_2) ? My input is two dictionaries that have string keys
Possible Duplicate: python .rstrip removes one additional character i have a string vtype defined
Possible Duplicate: Python: Split string with multiple delimiters I have a program where I
Possible Duplicate: Circular (or cyclic) imports in Python I have class B that imports

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.