Formatting String Output
Use + as concatenation operator
Example
Write a program to input a number and show factorial of it
Solution 1
import math
n=int(input("Enter a number"))
msg="Factorial of " + str(n) + " is " + str(math.factorial(n))
print(msg)
Example
Write a program to input a number and show factorial of it
Solution 1
import math
n=int(input("Enter a number"))
msg="Factorial of " + str(n) + " is " + str(math.factorial(n))
print(msg)
Solution 2
Using format specifiers like %d, %s, %f
import math
n=int(input("Enter a number"))
msg="Factorial of %d is %d" % (n,math.factorial(n))
print(msg)
Solution 3
Using placeholders {} with format() function
import math
n=int(input("Enter a number"))
msg="Factorial of {0} is {1}".format(n,math.factorial(n))
print(msg)
Comments
Post a Comment