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.

Hello!

I am also having a similar problem with a similar outcome.

I am trying to display probability information without a leading zero (i.e., .27 vs. 0.27), but the text builder will not let me do this. I tried rounded the probabilities to full integers and then manually displaying a period as it’s own text item, but this has issues with displaying probabilities less than ten because it will not include a leading zero (ie., . 3 vs, .03). I tried changing the variable in excel to be text formatted, and manually added in a zero where they were needed - but when the variable went through the text component, the zero was removed again.

When I tried this code, with the {:03} changed to {:02}, I got an invalid syntax error. I also tried reading the variable as a string, but this also deleted the zeros I had added as text formatted excel values.

Any ideas or solutions would be greatly appreciated!

Hi @fnwalsh , if you just want to print floats without the leading zeros you could try the following in the text component:

$str(yourVal).lstrip("0")