Formatting string in python

Author: Al-mamun Sarkar Date: 2020-03-24 20:06:29

Formating string in python. The following code shows how to format string in python.

a = 23
b = 3.2352354
c = 'Bangladesh'

print("Int %d" % a)
print("Float %.2f" % b)
print("Country %s" % c)


a = input()
b = input()

print("Input are : ", a, " and ", b)
print("Input are : %s and %s" % (a, b))

print(a + ' ' + b)
c = '-'.join((a, b))
print(c)

Output:

Int 23
Float 3.24
Country Bangladesh
>> 12
>> 23
Input are :  12  and  23
Input are : 12 and 23
12 23
12-23

 

Use variable inside a String:

name = "Jone Doe"
gpa = 5.00

print(f"His name is {name} and GPA is {gpa}")

Output:

His name is Jone Doe and GPA is 5.0