String Length
May 01, 2018
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.
Recent Entries
- Entropy November 13, 2025
- The Unquiet Past November 12, 2025
- King Sorrow October 22, 2025