What is the functionality of n?

Asked

Viewed 21,141 times

6

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

5 answers

13

Python uses the character \n for line breaking.

Note that even if it is composed of two characters, it represents only one in the ASCII table: character 10. It is also known as ASCII LF, where LF comes from line feed. The presence of the backslash preceding the character n means escape sequence. In other words, we are stating that this n is not the letter "genus", but rather the line break command.

As commented on 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:

print("Ola\nMundo")

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

Ola
Mundo

But it is not always this way out that we wish to get, as it may happen from your string use the backslash of course. For example, the path to a directory in Windows could be:

C:\temp\novos\foto.png

If you do it in Python:

print("C:\temp\novos\foto.png")

The output generated would be:

C:  emp
ovosoto.png

That’s because the escape sequence \t represents a tabulation and therefore a spacing is inserted within the string; the sequence \n is a line break and the sequence \f is not recognized, so it is ignored. In this case, you can add a prefix r to his string to indicate to Python that you want your text the way it is, without analyzing it:

print(r"C:\temp\novos\foto.png")

This way the output will be exactly the expected text:

C:\temp\novos\foto.png

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

print("C:\\temp\\novos\\foto.png")

Thus, the result produced is identical when using the prefix r in the text.

In Python there is still the escaped sequence \N that should not be confused with \n. In the first the N is uppercase and in the second it is lowercase. It may seem that it makes no difference, but it does. When used the prefix u in the desired text, indicating to Python to interpret the text as Unicode, the sequence \N is used to insert characters through their name - in Python 3, the prefix u is unnecessary. For example, when doing:

print(u"\N{BLACK SPADE SUIT}")

The output generated is:

5

BREAK LINES.

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

quebra de linha

1

\n used for skipping 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 operation of the character in Python has the same behavior in JS, so here I leave 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;
 
console.log(result);
 
console.log(result2);

1

is a type of break line that is used to "break" the line of code thus

print("ola mundo \r\n exemplo")

the exit will be like this

ola mundo
exemplo
  • OK thanks for the warning.

0

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

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

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

The backslash character(\) is called Escape, I mean, you can add "special things to your code." Normally every file containing text has this escape \n at the end of each line.

Ex:

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

ASCII TABLE

Okay, but what do you mean by that?

1KB de .jpg valem mais que 1024MB de .txt

Now let’s go to hexadecimal Character escape. Okay I know, now you understand all hehehe, but calm down.

In the picture we have that DEC 10 = char LF(and some explanations as well as new line(new line)). Ok, so what?

Now let’s take the values HEXADECIMAL and work them.

Exemplifying the hexadecimal Character escape( x):

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

Final example:

print("\x4f\x6c\x61\x21") # rode este código e entenderá melhor.

Browser other questions tagged

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