"Python syntax error" (for Builder, but not coder) in 'text' field, whenever I try to concatenate a variable with string or use string formatting. Concatenated strings work with code but not in builder

macOS Mojave 10.14.6
PsychoPy version 3 2020.2.10

Hello, I have been using PsychoPy to build a task and have had a persistent issue in the text component with concatenating/formatting strings. I have a variable “propString” that was a number that I converted to a string. I wanted to print it on a line with a percent symbol, " %". I emulated this video and other descriptions online and my code looks no different than how you typically concatenate a string:
Screen Shot 2020-12-13 at 6.55.13 PM

As you can see, I receive a Python syntax error. This occurs even if I use single quotations or double, and if I try other ways of string formatting such as:

"{} %".format(propString)

However, when I concatenate/format the string in the coder as seen below, I receive no such error and my experiment runs fine with the concatenated string:

percentage.setText(propString + " %")

However, that’s not ideal since changes that you make in the coder don’t transfer to the builder/psyexp file. So I came to a different workaround where in a code component, I concatenate the string there rather than in the text component (see below). This works fine and achieves the desired result:

propString = str(proportion) + " %"

This is quite odd that the builder is preventing me from concatenating/formatting strings, but this is something that other users are clearly able to do. Furthermore, it’s something I can easily achieve with code but not in the builder so it leads me to suspect that something wrong is going on with my builder.

I have tested this with other strings besides “%” as I thought maybe it was a symbol issue and this problem occurs if I try to concatenate the variable with any string. The variable is already a string, by the way.

Can anyone provide me with any advice or recommendations on how to fix this problem in my Psychopy builder?

Hello,

I did not test this in PsychoPy, but in python you need to escape the % with %, so it is %%. If you are using .format, it should be

percent = 12
print ("Percentage: {}%".format(percent))

See here

Keep in mind that the .format-style does not auto-translated to JavaScript.

Best Jens

1 Like

Thanks for the tip. The above issue in the original post was occurring even when using characters that are not “%”, so that’s part of what was so confusing. Oddly, I have since created a new PsychoPy file and the issue is no longer occurring and I can concatenate a variable (e.g. $propString) with a character string, (e.g. "Hello world)… "$propString + “Hello world”). Before, in the other PsychoPy script, I was receiving that error, "Python syntax error in field text" even for characters that are not the special symbol “%”. Still unsure what source of the issue was in previous script and why I couldn’t concatenate strings there but can now, but hey, it’s working now so I’m happy.

Try $str(propString)+’ %’ in the text component.