String methods


#1

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


#2

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.


#3

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


#4

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.


#5

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 ?


#6

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


#7

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


#8

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