'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"
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 1:[1]
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(input_string)
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.
Solution 2:[2]
len(input("what is your name?"))
returns the length of the input string as you can see in the official documentation. The length is stored as an int.
Solution 3:[3]
The expression inside of the len function is a str (it could be other data types as well - for example, a list), but that does not mean that the whole expression is still a str.
Let's break down what happens when Python sees the line of code:
print(type(len(input("what is your name?"))))
- Python starts by getting the innermost string, which is the literal
str"what is your name?". - Python then calls the
inputbuiltin function on thisstr, and this returns anotherstr, inputted by the user. - Next, the builtin function
lenis called on the string returned by theinputfunction. The value inputted intolenis still of typestr, but the new value returned bylenis now anint. The original string is discarded and we are left with anintinstead. - After this, the builtin function
typeis called on the newintvalue and returns that it is an integer. Note, it is giving the type of theintvalue returned by thelenfunction, not thestrvalue returned by theinputfunction. - Finally, the builtin function
printis called on the newtypevalue, and prints the output<class 'int'>. Theprintfunction returnsNone
This can perhaps be seen more clearly using a diagram:
----EXPRESSION---- ----OUTPUT----
str: "what is your name?"
|
| function: input -------> what is your name?
v
str: value inputted by user
e.g. "goose"
|
| function: len
v
int: length of value inputted
e.g. 5
|
| function: type
v
type: <class 'int'>
|
| function: print -------> <class 'int'>
v
NoneType: None
Solution 4:[4]
considering your example
x=input("what is your name?")
The inputs are stored in the variable x. Remember that when using input(), irrespective of the data type of the user input, the user’s value input is stored in some variable (x in your case) in the form of a string
``string as input character as input integer as input
now coming towards the second part of your question regarding
x=len(input("what is your name?"))
it should be clear that len function always gives the length or the number of characters of the parameter passed as the input to it, so it is clear that the number of characters will always have a data type of integers.
Example : suppose x = stack overflow, which is the user's input. since the string stack overflow has 14 characters including the space, it's length will be 14, and 14 is an integer. so the data type of the len function applied to it will be integer.
same is the case if a single character is given as an input
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Erik Wessel |
| Solution 3 | |
| Solution 4 |
