PRINT FORMATTING IN PYTHON (Display text in style)

Formatting Text Styles in Python


We always want our programs to be user friendly and more attractive. For this we always try to prompt our messages more meaningful and attractive. For this we can print the text messages and calculated output in different formats.
     Actually, in programming print formatting means to make the output colorful and use different text styles. We should write code on our own to style our text. This article will help you to learn about different formatting we can apply to print() function in python platform.

The examples illustrated in this article are based on Python 3.7.


print formatting in python

1. String addition

 String addition is also one of the print format. In python we can add the strings. This addition can also be done with the variables which represents strings data type.
Example:
name=input("Enter your name")
print("Hello, "+ name)
Similarly, numbers can be added inside print() without declaring addition variable.
     Example:
 print(12+7)


2. Using comma print formatting with strings (and numbers)

  In python the print() function supports separation of strings using commas which can be useful to combine several output values. To use this method the string output should be stored in variables.
Example: First lets create a string variable and store different string values in them. Then, we will use this print formatting method.
  name="Newton"
  country="Nepal"
  print("I am",name,"from",country)
Similarly, print formatting using comma can be used with combination of strings and number too.
 Example:
  name="Jack"
  age=input("Enter your age")
  print("Hi,",name,"you are just",age,"wait until",18)

(Note: The comma used for separation isn't displayed on the screen. A blank space is created by default between these strings, so you don't need to put extra space between them.)

3. Using quote inside quote

   Sometime we may have to use single quote inside strings or in the message to be displayed in the print() function or input () function. In these case use of odd single quote can cause errors in your quote and incase it works the code look confusing. In this case you have to use single quote inside double quote as shown in the example.
Example:
        print("This is Ram's House")

        a=input("What's your city name?")

(Note: In normal case also the double quote and single quote perform same function. So, you can use double quote too wherever you use single quote)

4. Using string method to format output

  This method is used to display output in fancier way.  str.ljust()str.rjust()str.centre()  are used to format output in style. This method makes the output stylish by shifting and repeated generation of a string.
  Example:
     msg = "I love PYTHON"

     print (msg.center(40, '#'))

     print (msg.ljust(40, '-'))

     print (msg.rjust(40, '-'))

Output of this code look like this:
####################I love PYTHON####################
I love PYTHON---------------------------------------
---------------------------------------I love PYTHON

This method can be used to align the output to the right, left or center. It can be done by replacing the character to be repeated by a blank space.
   Example: print(msg.center(" ")

There are more ways to format output in python which will be updated in the coming articles.
Comment down for any kind of confusion or help.

Post a Comment

0 Comments