How to make a formatted print?

Asked

Viewed 47 times

1

Hi. I’m sorry about the basic question, but I started dealing with python a week ago. I’m writing a program that records candidates' votes in a binary file. But, at the time of displaying I am not finding a way to leave "beautiful". I would like to appear only the Name and the Number of votes. Follows code:

arquivo = open('C:\Users\Cassio\Documents\Python Programs\eleicao.dat', 'rb')
    print ('Os votos dos candidatos serao exibidos a seguir: ')
    for x in range(cont):
        aux = pickle.load(arquivo)
        print('O candidato(a): ', aux['NOME'], 'tem: ', aux['NUM_VOTOS'], 'votos.')
    arquivo.close()

Follow the exit code:

Os votos dos candidatos serao exibidos a seguir: 
('O candidato(a): ', 'CASSIO', 'tem: ', 50, 'votos.')

I would like it to be shown without the quotation marks and parentheses. I thank you.

EDIT: I was able to solve in a "grotesque" way, but it works. It follows code:

print'Candidato(a): '+aux['NOME']+' - '+str(aux['NUM_VOTOS'])+' votos\n'

Exit:

Candidato(a): CASSIO - 300 votos

1 answer

0

Could use the function str.format (retro-compatible with python 2.6+).

print('O candidato(a): {} tem {} votos.'.format(aux['NOME'], aux['NUM_VOTOS']))

A version compatible only for (Python 3.6+) would be using the syntax of f-strings python:

print(f'O candidato(a): {aux["NOME"]} tem {aux["NUM_VOTOS"]} votos.')
  • 1

    Thank you very much!

Browser other questions tagged

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