In Python, the underscore holds the result of the last executed expression.
In some cases, it is used to replace a variable that will not be used.
In your example, as you just need to loop n number of times without needing to know the value of each iteration, you can use for _ in range(n)
instead of for i in range(n)
.
You can find more information about the underscore in Python here: What is the purpose of the single underscore "_" variable in Python?
As for the strip
and split
methods, here is a quick explanation based on the Python documentation.
str.strip
: Return a copy of the string with the leading and trailing characters removed.
str.split
: Return a list of the words in the string, using sep as the delimiter string.
So from your example, your code takes the input of the user, removes any leading and trailing characters with strip
, and split
the input into a list of words.
For example, if the user input is Hello World!
, the result will be: ["Hello", "World!"]
Hope that helps!