What is the functionality of n?

Posted on

Question :

I’m learning to program in Python and some basic things still confuse me, for example this question I asked. How useful is n?


Answer :

Python uses the n character to break lines.

Note that even though it is made up of two characters, it represents only one in the ASCII table: the 10 character. It is also known as LF ASCII, where LF comes from line feed . The presence of the backslash preceding the character n means escape sequence. In other words, we are informing that this n is not the letter “êne”, but rather the command to break the line.

As commented in the question, some interesting readings:

In Python, all escaped sequences will be interpreted when present within a text ( string ). That is, if you ask Python to display the message:


The escaped string will be in a string and therefore will be parsed, generating the output:


But it is not always this output that we want to get, as it may happen that your string uses the backslash naturally. For example, the path of a directory in Windows could be:


If you do in Python:


The output generated would be:

C:  emp

This is because the escaped string t represents a tab, so a space inside the string is inserted; the n sequence is a line break and the f sequence is not recognized, so it is ignored. In this case, you can add a r prefix to your string to tell Python that you want your text the way it is, without parsing it:


In this way, the output will be exactly the expected text:


Another way is to escape the escape sequence (yes, you read it right). In fact, what is done is to escape the backslash character, indicating that that backslash should not be parsed as an escape character. This is:


In this way, the result produced is the same as using the r prefix in the text.

In Python there is still the escaped string N not to be confused with n . In the first one the N is capital letter and in the second one it is minuscule. It may seem like it does not make a difference, but it does. When using the u prefix in the desired text, telling Python to interpret the text as Unicode, the N string is used to enter characters by its name – in Python 3, the prefix u is unnecessary. For example, when doing:


The output generated is:



The code: print "Fisrt line!nSecond line!" has the following output:


is a type of break line that is used to “break” the line of code

print("ola mundo rn exemplo")

The output will be like this

ola mundo


n used to skip lines

nome ='Paulo'
profissao = 'estudante'
escola = 'estadual dourado'
idade = 18

print 'Nome: '+nome   + 'Trabalho: '+profissao +  'Escola: ' +escola

print 'Nome: '+nome   + 'nTrabalho: '+profissao +  'nEscola: ' +escola


The function of the character in Python has the same behavior in JS, so I leave here an example in JS for practicality.

  • Example in javascript
var nome ='Paulo'
var profissao = 'estudante'
var escola = 'estadual dourado'
var idade = 18;
var result = 'Nome: '+nome   + 'Trabalho: '+profissao +  'Escola: ' +escola;
var result2 =  'Nome: '+nome   + 'nTrabalho: '+profissao +  'nEscola: ' +escola;



I’m learning to program in Python and some basic things still confuse me, for example this question I asked. How useful is n?

In any text file, the line actually ends with a n . It does not appear because it is omitted, precisely because it has this function.
It corresponds to 10 in Decimal that is 0xA .

Python is a high-level language so many concepts are sometimes left out. OBS: I am not judging anyone, I will be clear and objective.

The counter-bar character ( ) is called Escape , that is, you can add “special things to your code.”
Normally every file containing text has this n escape at the end of each line.


print("Fiat              - n lux") # Perceba que só mudou a linha após o n


Ok, but what do you mean by that?





print("Capacidade   - x0a 10kg  x0b") # agora podemos ver mais por trás dos bastidores

Final example:

print("x4fx6cx61x21") # rode este código e entenderá melhor.


Leave a Reply

Your email address will not be published. Required fields are marked *