How to break line inside python dictionaries?

Asked

Viewed 376 times

1

socios = {}    
def cadastro():
         addSocio = input("Digite o nome do sócio que deseja cadastrar: ")
         telefoneSocio = int(input("Digite o número de telefone do sócio (DDD + NÚMERO): "))
         cpfSocio = input("Digite o CPF do socio: ")
         socios[addSocio] = ("|telefone: {}| |CPF: {}|".format(telefoneSocio,cpfSocio))
         print("Cadastro realizado com sucesso!")

I am a beginner in python and I am trying to create a membership register using dictionaries according to the code above, I wanted to know how to break line after each partner’s Cpf, so that when I use the print the data are printed on the screen in an organized way.

  • I think the line break in Python is with \n, try something like this: "telefone: {}\nCPF: {}"

1 answer

1


The format being saved as an object python, if the goal is only to display on the screen in a more organized way, we can convert to json and thus display:

import json
#seu código
print(json.dumps(socios, indent=4))

Upshot:

{
    "Marcelo": "|telefone: 44999451668| |CPF: 07786794884|",
    "Juarez": "|telefone: 44995585585| |CPF: 48857785958|"
}
  • Thank you very much, that’s exactly what I’ve been wanting.

Browser other questions tagged

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