0
What is str method?
Is it used for changing one data type to string type of data...
4 Antworten
0
yes, you are right.
str(5)
makes your number 5 as string 5
it's called type conversion
0
But they have used in calculator... And it makes no sense in changing result into string... @sundar
0
in certain cases you need to convert your number as string.
for example:
favnum=int(input("enter your fav number:")
print('my favorite number is "+str(favnum))
here we are getting user input as integer and storing it to a variable. now if we normally concatinate the variable with + it will throw error (since the type is int) so, we need to convert this type to str to concatinate
It's just a simple example.
however
print("my fav number is", favnum)
would work without error
anyhow type conversion is necessary for some complex programs
hope u got it...
0
type conversion actually pretty important to inform user if you use print()
because :
x = 2
y = 3 + 2
print(x + y)
print(str(x)+str(y))
that will result:
7
23 + 2
if you print string + interger it would be an error there.