Ask coding questions

← Back to all posts
print function in python

can we add "str" and "int" in one single print function without using .format .if no plz explain.
print("hello World")
x =int(input("Enter any number:-"))
y=int(input("Enter any number:-"))
print("the sum of"+x +" and "+y +" is" +z )


Change the int to a string by doing str(x). Then you can concatenate them




Yes! Use str(i) for changing an integer i into string


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.
Example 1:

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.

Example 2:

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 int and str will. You can use the methods __str__ or __repr__. So to convert the integer to a string you would do the following.

Example 3:

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.

Example 4:

There you go! Hopefully this helped you or you found another solution that you liked!


@PYer hi pyer
as mention in example 3
print("the sum of"+x.str() +" and "+y.str() +" is" +z.str() )
as i m beginer so don't understand the this function (.str() ) why you use one dot , 2 underscore str etc..
and thanks your precious time taken for reply to my problem