print function in python
can we add "str" and "int" in one single print function without using .format .if no plz explain.
x =int(input("Enter any number:-"))
y=int(input("Enter any number:-"))
print("the sum of"+x +" and "+y +" is" +z )
Yes. It is possible to use the string method
.format to bypass the TypeError exception. You seem to know about how you can convert strings to integers, and vice versa so I will show you a few other ways you could solve your problem.
There. By making to separate pairs of variables
a, x and
b, y you have the string stored in one, and the integer stored in another. Then you can just print out the strings and calculate with the integers.
This one here is a little more compact. You create a seperate copy of these variables and add them straight into the string without making them a seperate variable.
If the reason you want
.format is because you don't like the way it looks, (Weird reasons, but I have met people who don't) you can use two other methods on it. This may not work, but most fully developed classes, like
str will. You can use the methods
__repr__. So to convert the integer to a string you would do the following.
This may look a bit weird to you, but I do know some people who think this looks more proffesional and prefer this way, even though it is longer to type :).
But to answer your question, yes, it is possible to use the
format method to not need to do any conversion. Yours should look like the following.
There you go! Hopefully this helped you or you found another solution that you liked!