What is str method? | Sololearn: Learn to code for FREE!
0

What is str method?

Is it used for changing one data type to string type of data...

27th Jul 2016, 2:28 PM
Pratham Gupta
Pratham Gupta - avatar
4 Respostas
0
yes, you are right. str(5) makes your number 5 as string 5 it's called type conversion
27th Jul 2016, 3:47 PM
sundar
sundar - avatar
0
But they have used in calculator... And it makes no sense in changing result into string... @sundar
27th Jul 2016, 3:51 PM
Pratham Gupta
Pratham Gupta - avatar
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...
27th Jul 2016, 4:02 PM
sundar
sundar - avatar
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.
28th Jul 2016, 4:58 AM
Erlangga Ibrahim
Erlangga Ibrahim - avatar