Concatenation in Python

Asked

Viewed 51 times

-4

I’m having trouble concatenating the result of a string with a string.

velas[0] = '\033[0;32;42m   g   ' if velas[0]['open'] < velas[0]['close'] else '\033[0;31;41m   r   ' if velas[0]['open'] > velas[0]['close'] else '\033[0;37;47m   d     ' 
        velas[1] = '\033[0;32;42m   g   ' if velas[1]['open'] < velas[1]['close'] else '\033[0;31;41m   r   ' if velas[1]['open'] > velas[1]['close'] else '\033[0;37;47m   d     '
        velas[2] = '\033[0;32;42m   g   ' if velas[2]['open'] < velas[2]['close'] else '\033[0;31;41m   r   ' if velas[2]['open'] > velas[2]['close'] else '\033[0;37;47m   d     '
        
        cores = velas[0] + ' ' + velas[1] + ' ' + velas[2]  
        
        print(cores)

When concateno with '+' it brings the line painted as the result of '\033[0;32;42m g ', but does not separate with white space (brings everything stuck). Concateno with ',' cores = velas[0],velas[1],velas[2] he brings the string '\033[0;32;42m g ' space-separated.

I need the result of '\033[0;32;42m g ' be printed and separated by a blank space. Someone can help, sorry, I don’t know if I made myself clear.

1 answer

1

You can use the method string.()

The syntax of the command is:

resultado = str_do_caractere_separador.join(lista)

Follow an example:

cores = ' '.join(velas)
  • I tried the command as you suggested and not yet rolled, keep bringing everything together.

  • I can’t exactly reproduce the code of your question. Could you explain to me better how this section works: if velas[0]['open'] < velas[0]['close'] else '\033[0;31;41m r ' if velas[0]['open'] > velas[0]['close'] else '\033[0;37;47m d '

Browser other questions tagged

You are not signed in. Login or sign up in order to post.