I have the following expression:
namer = name[-4:]
Where name
gets a name, but what does this [-4:]
mean?
I have the following expression:
namer = name[-4:]
Where name
gets a name, but what does this [-4:]
mean?
This is a slice (slice). The code in question returns the last four characters of the string.
A slice follows the [começo:fim]
pattern. começo
and fim
are indexes starting from zero. The character of the first index is included in the slice, since the last index character is not, i.e., the slice includes começo
but does not include fim
.
Then [-4:]
means "return the slice from the fourth last index to the end of the string".