Adding a space in free response

Hello everyone,

I am looking for help when trying to add in a space on a free response question in my experiment. At first I was having the issue where the word “space” was just butting put in there (ex. itspacewouldspacelookspacelikespacethisspace) but then figured out how to remove the word being put int. Now I am having the issue where nothing is changing (ex. itwouldlooklikethis). I have tried searching previous forums and tutorials online but cant find anything that has solved the issue. I am sure this is a simple solution and something I am just missing since I am not familiar with python coding but any help would be greatly appreciated. Thank you! I have added in relevant information below.


In begin routine:
respDisplayThree= “”
maxDigits = 500

          #key logger defaults
          last_len = 0
          key_list = []

In Each Frame:
#if a new key has been pressed since last time
if(len(keyresp_3.keys) > last_len):

        #increment the key logger length
        last_len = len(keyresp_3.keys)
        #grab the last key added to the keys list
        #check for backspace
        if("backspace" in key_list):
            #if we have at least 1 character, remove it
            if(len(key_list) > 0):
        #if enter is pressed then...
        elif("return" in key_list):
            #remove the enter key

     #if space is pressed then...
        if("space" in key_list):
            #add in a space rather than "space"
        if(len(key_list) > 1):
            respDisplayThree += ' '
        #now loop through and remove any extra characters that may exist
        while(len(key_list) > maxDigits):
        #create a variable to display
        respDisplayThree = ''.join(key_list)

In end routine:
thisExp.addData(‘subjSuggestion’, respDisplayThree)

Thanks in advance for any and all help!

Could you use .pop to remove the space and .append to add " "?

I did try key_list.pop() (just like that) and instead of adding a space it removed the previous letter. When doing key_list.append(" ") it put a space after each letter.


did you try .replace instead of .remove()?

Best wishes Jens

I meant within the if(“space” in key_list): clause

Is this what you were suggesting? I have tried this and is has not added in a space.


I have tried this but it did not add a space. Is this how you were suggesting the formatting of the code be? Sorry just want to make sure I am even writing this correctly for what you are suggesting


if "space" in key_list:
     key_list.append(" ")

Don’t append a space just because the length is > 0

Thank you for clarifying! I have done this but then received the following error message:
TypeError: ‘str’ object cannot be interpreted as an integer”


 #if space is pressed then...
    if "space" in key_list:
        #add in a space rather than "space"
       key_list.append(" ")

This worked. Thank you SO much for all the help!

Sorry – I meant

if "space" in key_list:
     key_list.append(" ")

Anyway – you’ve succeeded using remove

Hello avogel,

the syntax for .replace is different. You need to specify the old value and the new value and store the result in a new string. See the example:

stringSpace = "Thisspacestringspacecontainsspace"
stringNoSpace = stringSpace.replace("space", " ")

So, you take the whole string not just a key and replace space with " ".

Best wishes Jens