String Length
Python’s len()
method can be used to easily find the length of a string. It’s a simple and quick way to measure the length of a string (the number of characters) without having to write a lot of code. The syntax for using the len()
method is fairly straightforward, and hard to mess up. Even I can manage it.
from sys import argv
script, file = argv
source = open(file)
indata = source.read()
print "The file called '%s' is %s bytes in length." % (file, len(indata))
Remember that len()
calculates the string so it can be used for characters too. You could ask Python to count the letters in a word or sentence.