Sunday, September 16, 2018

Python Ninja bootcamp 6-Print Formatting

Learn Python like a Professional! Take You from 0 to Hero




In this Python lecture, we will learn about the Print formatting.


"The various way of presenting the output in print statements".



Print formatting


Let's see some example-

print('Welcome to Python Ninja Bootcamp ')
>> ' Welcome to Python Ninja Bootcamp '


String Formatting


We can use %s to formart the into print statement.


x='Students'
print('Place my variable here: %s' %(x))
>> Place my variable here: Students

# we can the variable x to z 
z='Students' 
print('Place my variable here: %s' %(z))
>> Place my variable here: Students


# we can also convert number object to string format
z=11.25
print('Place my variable here: %s' %(z))
>> Place my variable here: 11.25


Float Formatting


We can %f to format in floating statement


# In float format % n1.n2f
# n1 stands for total number of digits that Strings  should contain
# n2 stand for total number of digits after decimal 

print('Floating point number: %1.1f '%(11.543))  
>> Floating point number: 11.5

# 1.2%f -> Two digits will be display after decimal
print('Floating point number: %1.2f '%(11.543))
>> Floating point number: 11.54

We can use print function to print both the strings
# 1.10%f -> Ten digits will display after decimal
print('Floating point number: %1.10f '%(11.543))
>> Floating point number: 11.5430000000

#1.3%f -> Three digits will be display after decimal
print('Floating point number: %1.3f '%(11.543))
>> Floating point number: 11.543 


Multiple formatting


# format objects into the multiple string formats
print('First: %s, Second: %s, Third: %s' %('hi','Tamta',5))
>>  First: hi, Second: Tamta, Third: 5

# we have to define same objects multiple to assign same value in string formats 
print('First: %s, Second: %s'%(2,2))
>>  First: 2, Second: 2



String .format( )


The best way to format object into the string for print statements

Let's see some example

# we have to define the objects once to assign same value in string formats multiple times 
print('First: {x}, Second: {x}'.format(x='Numbers'))
>> First: Numbers, Second: Numbers

# we can also add multiple number of string format statement  
print('First: {x}, Second: {x}, Third: {y}'.format(x='Numbers',y='Strings'))
>> First: Numbers, Second: Numbers, Third: Strings

# we can change the sequence of string format statement
print('First: {x}, Second: {y}, Third: {x}'.format(x='Numbers',y='Strings'))
>> First: Numbers, Second: Strings, Third: Numbers




EDITS ARE WELCOMED!!

In the next Blog, we will discuss List. 

https://sngurukuls247.blogspot.com/2018/09/python-ninja-bootcamp-7-list.html

......................................................................................................................................

Follow the link below to access Free Python Lectures-
https://www.youtube.com/channel/UCENc9qI7_r8KMf6-_1R1xnw

Instagram-
https://www.instagram.com/python.india/

View the Jupyter Notebook for this lecture

Download the Jupyter Notebook for this lecture 



Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com

No comments:

Post a Comment