I'm learning to program in Python and some basic things still confuse me, for example this question I asked. How useful is \ n?
I'm learning to program in Python and some basic things still confuse me, for example this question I asked. How useful is \ n?
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:
print("Ola\nMundo")
The escaped string will be in a string and therefore will be parsed, generating the output:
Ola
Mundo
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:
C:\temp\novos\foto.png
If you do in Python:
print("C:\temp\novos\foto.png")
The output generated would be:
C: emp
ovosoto.png
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:
print(r"C:\temp\novos\foto.png")
In this way, the output will be exactly the expected text:
C:\temp\novos\foto.png
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:
print("C:\temp\novos\foto.png")
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:
print(u"\N{BLACK SPADE SUIT}")
The output generated is:
♠
is a type of break line that is used to "break" the line of code so
print("ola mundo \r\n exemplo")
The output will be like this
ola mundo
exemplo
\ 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.
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);
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.
Ex:
print("Fiat - \n lux") # Perceba que só mudou a linha após o \n
ASCII TABLE
Ok, but what do you mean by that?
Nowwegotothehexescapecharacter.OkIknow,nowyouunderstandeverythinghehehe,butcalmdown.
IntheimagewehaveDEC10=charLF(andsomeexplanationsaswellasnewline).Okay,sowhat?
Nowlet'staketheHEXADECIMALSvaluesandworkonthem.
Exemplifyingthehexadecimalcharacterescape(\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.