Just use replace
and replace all quotes with an empty string (which is the same as removing them):
print('Resultado ---> ', ''.join(lista).replace("'", ""))
With this, the simple quotes ('
) are eliminated from the final result.
If you want, you can also use a comprehensilist on and do the replace
on each item of the list separately:
print('Resultado ---> ', ''.join(s.replace("'", "") for s in lista))
Both print:
Result --> testeKey.Esc
Although in this case I find the first option simpler and more direct.
And if the character itself '
is typed?
I did a typing test a
, afterward '
, afterward ESC, and the list goes like this:
["'a'", '"\'"', 'Key.esc']
Note that the character '
is in double quotes.
Then the replace
above does not work as it also removes the '
that was typed and the final result is a""Key.esc
.
To fix this, we can create a function to treat this special case:
def replace_exceto_aspas(s):
if '"\'"' == s:
return "'"
return s.replace("'", "")
That is, if it is the character itself '
in double quotes ("
), I return a single quotation mark. Otherwise, I remove the single quotation marks.
Now just use the comprehensilist on, calling this function to all elements of the list, and finally joining everything with join
:
print('Resultado ---> ', ''.join(replace_exceto_aspas(s) for s in lista))
The result will be:
Result --> a'Key.Esc
Alternative
But maybe it’s simpler to change the way you pick up the character being typed:
def on_press(key): # tecla pressionada
try:
lista.append(key.char)
except AttributeError:
lista.append(str(key))
If the key you press has the attribute char
- as is the case with alphanumeric characters - its value is added in the list (only the value of the character itself, without the quotation marks). If another non-alphanumeric key (like the ESC), he falls in the except
and is added the value as you were already doing.
So you can just do ''.join(lista)
, without having to worry about removing the quotes (no longer need the replace
, or the function I created above).
I appreciate your help and time to answer that question. Your reply was accurate and, coincidentally, I started using the indicated alternative. Thank you. :)
– Wilson Junior