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
- Filter First, Think Later February 21, 2026
- Five Hundred Bugs That Fuzzers Missed February 20, 2026
- Gemini 3.1 Pro and the 0.1 That Matters February 19, 2026