Is it good practice to use n to skip lines or to use a print() alone?

Asked

Viewed 148 times

0

Hello,

I already know that adding n to a string will cause a new line to start, but when I want to skip a line I should use n at the end/beginning of a string or should put a print() alone?

For example:

palavra = input("digite uma palavra: ")

for letra in palavra: 
  print(letra, end=" ")

print("Isso é um teste")

in that case let’s say I want to separate palavra of "isso é um teste" by two lines.

I should do print("\n\nIsso é um teste") or put two prints before print("isso é um teste") ?

  • 1

    This is a very strange question, because in practice it makes no difference. I believe that \n is a better option because it is leaner and without impairing the readability of the code

  • Which is more compact I know, but what if I need to skip 3 lines? then I would have to do n n n. I don’t know about you, but this way it looks weird.

  • Just do print('\n' * 3) - will print 3 \n (plus the \n that the very print already includes at the end)

  • Mmm, good idea. I would never have thought of that kkkk

  • It depends on the situation. There will be situations that will be necessary to use a "\n". In other situations the "\n * m" (m = integer) and in other situations it will be necessary "print()".

1 answer

0

In those cases I always worry about doing the way that makes the code more legible, if you only need to skip two lines use:

print("\n\n Meu texto")

It doesn’t look bad, but if the text is something much bigger, breaking in some prints may be the best output

print("Bem vindo ao meu programa")
print("Selecione a opção abaixo")
print("1")
  • Vlw by tip. <3

Browser other questions tagged

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