Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading