Write Some Simple Programs in Python For Formatting Number And Strings

Hi in this article you will learn multiple ways of formatting a number and a string using python.  So let's try to look into all types of formatting first is using the percentage next is using the flower braces And finally use the format function call to format a number and string.

Below are the methods
  1. %
  2. {}
  3. .format()

Let's use all three methods and try to Demo a simple Python program.

 

 

Write Some Simple Programs in Python For Formatting Number And Strings

 


Write Some Simple Programs in Python For Formatting Number And Strings

Let's take three variables: integer floating point and the string that is  number,  fraction and text.  We will be using these variables to store the values by asking a user to enter a number, floating point and string.

Let's use a normal formatting to print the  integer floating point and a text using the percentage (%) as shown below

number = int(input("Enter a Integer Number : "))
fraction = float(input("Enter a Floating Point Number : "))
text = input("Enter a String : ")

print("\nNormal Formatting...")
print('%d %f %s' %(number, fraction, text))





Now let's use another type of formatting  that is used to extend the length of the integer, float, and string variable as shown below

print("\Formatting using %.5d %.5f %.5s ...")
print('%.5d %.5f %.5s' %(number, fraction, text))





Now use the braces to format the integer floating point and the string variables as shown below

print("\nNormal Formatting using Flower Brackets ...")
print(f"{number} {fraction} {text}")





After formatting using the flower braces use the format() function call to format the numbers and string values.

print("\Formatting using .format() function ...")
print("{0} {1} {2}".format(number, fraction, text))





Complete Python Program
========================


number = int(input("Enter a Integer Number : "))
fraction = float(input("Enter a Floating Point Number : "))
text = input("Enter a String : ")

print("\nNormal Formating...")
print('%d %f %s' %(number, fraction, text))

print("\nFormating using %.5d %.5f %.5s ...")
print('%.5d %.5f %.5s' %(number, fraction, text))

print("\nNormal Formating using Flower Brackets ...")
print(f"{number} {fraction} {text}")

print("\nFormating using .format() function ...")
print("{0} {1} {2}".format(number, fraction, text))

 

 


Output 1
=============

Enter a Integer Number : 12
Enter a Floating Point Number : 75.55
Enter a String : code with tj

Normal Formatting...
12 75.550000 code with tj

Formatting using %.5d %.5f %.5s ...
00012 75.55000 code

Normal Formatting using Flower Brackets ...
12 75.55 code with tj

Formatting using .format() function ...
12 75.55 code with tj


    


Output 2
===============

Enter a Integer Number : 19
Enter a Floating Point Number : 45.756
Enter a String : format numbers and strings

Normal Formatting...
19 45.756000 format numbers and strings

Formatting using %.5d %.5f %.5s ...
00019 45.75600 forma

Normal Formatting using Flower Brackets ...
19 45.756 format numbers and strings

Formatting using .format() function ...
19 45.756 format numbers and strings





Conclusion
=================

Execute the above Python program by  providing random integer,  floating point and string values  as shown in the output.

Comment it down below if you have any suggestions or queries regarding the above Python program.


Post a Comment

0 Comments