String methods


Why does the code lower(variable_name) throw an error when the code is run and why not len(variable_name)?


len(variable_name) is a function that is not part of the strings. It is a function that exists somewhere in python where you can put a string in and it would give you the length back of the string.

print len("Hello World!")
#output   11

lower(variable_name) does not exists in python. It is written as

variable_name = "Hello World!"
variable_name.lower()    <---
print variable_name
#output  "hello world!"

I do not know why it differs from each other tho. They both seem to work just for strings. If anyone can add the reason for the this context that would be great.


len() is a built-in function which works for multiple data types:

len(['a', 'b', 'c'])

while .lower() is a method belonging to specific data type (string in this case), but to grasp this you need to understand classes


The Learn Python: Introduction to Classes course presents detailed material on distinctions between global functions and methods that belong to definitions of specific types of objects.


I figured as much but i wasn’t sure. I tried it with an integer but that didn’t seem to work. Guess it only works for iterators ?


multiple is not the same as all. An integer is a single value. So why would you want the length?


I expected it would return the length of how many integers the integer consist of.
for example:

input: 9
output: 1

input: 8673
output 4


no, the “length” would all be one. Integers take up a single byte (several bits, zeroes and ones)