2

Is there a better way to insert variables into a string?

Something, say, less repetitive than...

with open('zlog.txt', 'a') as log:
    log.write('[ %s ] Finished scrolling.\n[ %s ] #%s clicks.\n[ %s ] Number of Elements: %s\n' % (finish_time, finish_time, str(x), finish_time, str(len(elements))))

I am constantly reusing finish_time in order to timestamp entries in a log.

... % (finish_time, finish_time, str(x), finish_time, str(len(elements)))

10
  • @Dark my question isn't so much about inserting variables into a string, as the title may suggest, but rather about the most concise way(s) to insert repeated variables Commented Jun 26, 2018 at 6:06
  • A really simple option is to use do t = finish_time and then you can use t, t, x, t, len(elements). (Also, you don't need str(x) or str(len(elements))—passing an argument to a %s automatically calls str on it.) Commented Jun 26, 2018 at 6:09
  • @abarnert i'd rather have a more descriptive name for the variable, but thanks for the info about the string conversion! Commented Jun 26, 2018 at 6:10
  • @Anthony check the updated links :). Commented Jun 26, 2018 at 6:10
  • 1
    @Anthony there are two links in the duplicates you can check both. The latter is the one you wanted. Commented Jun 26, 2018 at 6:12

3 Answers 3

3

Use positional argument specifier in format:

>>> '{0} {0}, {1}!'.format('hello', 'world')
'hello hello, world!'
>>>
Sign up to request clarification or add additional context in comments.

12 Comments

Oh, wow, that's awesome! Would the syntax be write(...).formate(...)?
In your example, it would be something like log.write('[ {0} ] ... [ {0} ] ... [ {1} ] ...'.format(finish_time, x, ...)). Remember format is a method of str, not a method of an object returned by write.
This has to be the best answer yet. I'll mark it as the answer if nothing else ends up coming in.
Do the indexes have to begin at 0 or is the number arbitrary?
It can be any order, so '{0}, {1}, {0}!'.format('hello', 'world') would yield 'hello, world, hello!', for example.
|
1

If you're using python 3.6 there's this feature fstrings,

x = 5
print(f'Argument: {x}')

which will give you:

Argument: 5

17 Comments

Ahh, the good ole template literal? We have those in JS. Unfortunately I'm not using Python 3 since I'm not sure if I'll be able to use it in my production environment :(
But this doesn't help reduce the repetition. The OP's example would require {finish_time} three separate times.
Ah. I see. When I answered this question the title said 'Inserting Variables into String' I see it has changed after I posted this answer.
@Anthony What's your production environment? The latest LTSs from Ubuntu and RedHat/Centos come with Python 3 only. The previous ones came with both 2 and 3. And they've had Python 3 at least as an optional packages for years now. I doubt you'd actually have a problem—but it's better to actually check than to just guess.
@Anthony If you have enough rep to go to chat, see the page about the Python room. Anyway, I've seen Selenium questions answered here. Scrapy, almost all of the questions are novice Python questions rather than hard Scrapy questions, so… not sure if there really are many experts to help with the latter…
|
0

I love to do it using format method:

'my-var-value : {}'.format(variable)

format able to read any object or data type and pass it to string directly

format able to pass variable to string without index numbering

'my-var 1 : {} & my-var 2 : {}'.format(var1,var2)

8 Comments

So you don't even need to use an index in the parenthesis? This will be handled automatically? How then will they know which ones are going to be repeated and which ones are unique?
they know it by the order of the variables inside the parenthesess. the first one indexed 0 and the next one will be 1. you can read more indepth explanation in their string formatting guide
This doesn't help with repetition at all. The first {} is equivalent to {0}, the second to {1}, etc. Very convenient when you don't have repeated arguments, but it doesn't solve the OP's problem.
the first time I read your question it says : How to insert variable to a string. And it was edited 6 mins ago.
@Willysatrionugroho you must've misread my question the first time. you can view the edits i've made the question! your answer is still interesting and useful nonetheless
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.