What is the relation of len() function with int data type?

Advertisements

I’m writing a simple Python program to find the data type using type function.

x=input("what is your name?")
print(type(x))

It gives the output:

<class 'str'>

But when I add the len function"

x=len(input("what is your name?"))
print(type(x))

It gives the output:

<class int>

why is that? Can someone explain to me? Does len function change it to int type? what I have been told is len function works with string types only if we wanna find out the number of character in string.

>Solution :

The len function returns the length of the string, which happens to be an integer.

When the object is a string, the len() function returns the number of
characters in the string.

Learn more about the len function here: https://www.w3schools.com/python/ref_func_len.asp


To clear up things :-

input_string = input("what is your name?")
input_string_length = len(x)

print(type(input_string)) # <class 'str'>
print(type(input_string_length)) # <class int>

Thus, as you can see, the input string is not affected in any way and is not converted to the int type.

Leave a Reply Cancel reply