Hello, I have the following code:
def abs_string(string):
if string.startswith('-'):
return string[1:]
return string
which works well, that is, I send a string as a parameter if it contains a mid-hyphen, it removes it and returns only the string .
Well now my doubt within the code has the following:
string[1:]
what does this number that goes inside the brackets mean or what is that called in python thanks in advance.
The number simply represents the index of the character in the string. A string (
str
) like a list (lista
), a tuple (tuple
) or a byte string (bytes
) is a set of ordered elements (UTF-8 characters) and indexable. Each element is numbered starting from 0:so that you can access a given character using its index and the syntax
objeto[indice]
:Python also allows you to use negative indices, so that -1 refers to the last element, -2 the penultimate, etc:
On the other hand, the syntax you observe (
[1:]
) is known as extended indexing and performs a slicing of the iterable, which allows obtaining a new string with the content of part of the original string (or all of it, as we will see at the end) . That is, instead of getting a single character using its index, we get a set of characters from the string using a range of indices. The general syntax is:The initial index indicates from which element the cut begins. If nothing is indicated, it is assumed that the cut starts from the first element.
The final index indicates the index of the final element of the cut, which is not included in the cut. If nothing is indicated, it is assumed that it is up to the last element (included).
The step indicates how many indexes we count until we include a new element. For example
[::3]
take one of every three elements from the beginning to the end of the list. If nothing is indicated, it is 1 by default (take the elements one by one from the initial index to the end). The concept is really the same as inrange
, we start with the initial index of the slice and in each "iteration" the next index is generated by adding the step to the previous index:Given the possibility of using positive and negative indices as slice boundaries, things get a bit more complicated. It is perhaps the most complicated argument to understand and explain, below are some examples.
All three arguments must be integers.
Obviously you need at least one
:
to perform a slicing, otherwise we only do an index on one element. The second:
, the one that separates the final index from the step, is optional, if we do not determine a step it is not necessary.Therefore, in your case
[1:]
it indicates that a cut of the original string is made starting from the element with index 1 (the second character since the indexing starts at 0) until the end of the string. With this you get a new string without the-
, which is the first character and with index 0 therefore:Some examples of slicing:
Unlike simple indexing, an
IndexError
if there are invalid indices in the slicing range does not occur:The method that allows indexing and slicing to be used on an object is
__getitem__
, which we can implement in our own classes if we want to support them.This syntax is common to any other object that supports indexing, such as lists, tuples, etc. In mutable objects such as lists they have some extra applications, for example we can eliminate part of the elements of a list with
del
or perform assignments:Another way to do slicing is by directly creating objects
Slice
using the builtinslice
.cad[:7:2]
is actually translated toslice(None, 7,2)
:In addition to their internal use, objects
Slice
are very important in libraries that work with arrays and matrices such as NumPy, Pandas, etc.By the way, if you want to remove all possible characters
"-"
that exist at the beginning of the string, you can usestr.lstrip()
:In this link you can find extensive documentation (in English) where each utility of the function
string
and is explained in detailsubstring
.To be precise, the text inside the brackets is called
String slice
and what it does is refer to the text fraction that you indicate within a text string, taking into account that it is written in the following way:I invite you to read the article so you can see many more uses of
String slices
within python, such as reverse values (variable[-4]
), example from the documentation:I hope it helps.