psychopy.org | Reference | Downloads | Github

Leading zeroes in text component

I am creating the MSIT task, which requires presenting three digits on the screen; in some cases the digits begin with zeroes (e.g. 020 or 003). I am just using the builder for this experiment, and I am importing the stimuli from outside files.
I have tried importing the stimuli (i.e. digits) from pre-formatted xlsx files as well as csv files, but they always show up without the leading zeroes in the PsychoPy window (e.g. 20, or 3 instead of 020 or 003). I have also tried surrounding the digits with quotation marks, which works in terms of the leading zeroes, but obviously I don’t want the quotation marks displayed on the screen. Perhaps there is a code component I could use to get rid of the quotation marks? Or maybe another easier solution?
Cheers.

Hi @bwinston, you can left-pad with zeros using Pythons format function. In your text component, used to present text on screen, use the following:

$'{:03}'.format(YOUR_VALUE) # pad with 3 zeros

The dollar sign is used in the text component to tell PsychoPy that this is code that needs to be evaluated.

1 Like

Cheers David! For some reason, I was getting an error with that code, but I used this notation, which worked fine:

$’%03d’ % MY_VALUE

Apologies for the simple question, and all the best.