Formatting string in python

Formatting string in python

Instructor-svgAl-Mamun Sarkar
Mar 24 , 2020

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

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram