Hey all, it’s me again! Learning about the len function this week in regard to strings (ie. print(len(firstName))).
I’m wondering when this would ever be a useful function to use. Obviously it’s a built-in function so it must have some important uses, but as somebody new to programming, I’m not sure I can think of any.
Let’s say you’re writing a command-line utility which converts .foo
files to .bar format, and rather than using a complex argument
parser you just want to have the old and new filenames supplied as
bare arguments to the command. You may want to provide a useful
message to the user, like so:
if len(sys.argv) != 3: # remember argv[0] includes the command name
print("Please provide the names of both the foo and bar files!")
It’s a very simplistic example, but more generally, len() is useful
any time you want to find out if an iterable object contains fewer,
more than, or exactly sum number of items. Or, you might use it to
report the length of some object:
print("%s records loaded" % len(records.processed))
There are countless more uses, but maybe len() could count them for
you too!
Hey @fungi! Thanks for replying. I’ll be honest: that first block of code means nothing to me. I’m working on it and hopefully I’ll get there eventually, but still very new to coding (as is likely obvious from my lack of understanding of where len could be used.
With that said, your second one makes more sense to me, though I am wondering if you could confirm my understanding that records.processed would be a predefined/determined variable. Is that correct?
With that said, your second one makes more sense to me, though I
am wondering if you could confirm my understanding that records.processed would be a predefined/determined variable.
Is that correct?
I imagined that records might be an instance object of some
fictional Records class with a class attribute named processed of type list which got appended to after each
entry of some set of input data was processed. To be honest I didn’t
put a lot of thought into the example, it was just meant to
represent a situation where you might have some countable set of
data and you wanted to report the len() of it to a user. Maybe
they know how many records were supplied to the program, but didn’t
know that some of them were unable to be processed, or perhaps they
were deduplicated at the time they were stored, or… too many
possibilities really.
For lists len returns the number of items in the list (also known as «list size» or «list length»). Ditto for other types of sequences, such as tuples or strings.
Hmmm… I don’t really know what you know, so I’ll try my best to make it as simple as possible.
len() is an very useful function. One way it can be used is to print each character of a string out.
Like this!
test = "Hello World"
for i in range(len(test)):
print(test[i])
The len function tells the program how many characters are in the string. Without it it would try to read past the end of the function. Hope that makes sense!