Good job! In the previous exercise, we unpacked the tuple returned from count_spaces_commas_stops()
into three variables: spaces
, commas
, and stops
. However, we didn't actually use the commas
variable at all. Instead of creating an unnecessary variable, we can use an underscore (_
). Take a look:
def count_spaces_commas_stops(text):
spaces, commas, stops = 0, 0, 0
for letter in text:
if letter == ' ':
spaces += 1
elif letter == ',':
commas += 1
elif letter == '.':
stops += 1
return (spaces, commas, stops)
input_text = 'In this example text, there are some commas, spaces and full stops. However, the text is not long.'
spaces, _, stops = count_spaces_commas_stops(input_text)
print('There are', spaces, 'spaces and', stops, 'full stops in the text.')
Notice that, this time, we didn't introduce the commas
variable when we unpacked the tuple. Instead, we used an underscore (_
). The spaces, _, stops
notation means I know the function returns a tuple with three elements, but I only care about two of them.
Naturally, you can use more than one underscore if you want to ignore multiple tuple values.