Control Statements or Control Structures or Control Flow
Control statements are used to control the flow of execution of program. The control is executed
in three ways.
1. Sequential
2. Selection/Condition
3. Iteration
In sequential, the statements are executed one by one.
In selection, the statements are executed based on condition
In Iteration, the statements repeated one or more than one time
if statement
if is a condition statement or selection statement, this statement execute a block of statements
based on condition or selection.
Python provide different syntax of if statement.
Simple if
If..else
If..elif..else (if..elif ladder)
Nested if
Simple if
If without else is called simple if
Syntax:
if <condition>:
statement-1 STATEMENT-2
statement-3
If condition is True, it execute STATEMENT-1,STATEMENT-2 and statemen- 3.
If condition is False, it execute statement-3.
if True: print("Hello")
print("Continue")
if True:
print("Hello") # this generate syntax error because indented block
if True: # this generate syntax error because indented block
Indent is a space
In python block is defined by changing indent.
A block cannot defined without any statements. pass
Pass is a null operation — when it is executed, nothing happens. It is useful as a placeholder when
a statement is required syntactically, but no code needs to be executed.
if True: pass
print("Hello") # find output
if True:
pass # null operation print("Hello")
print("Continue")
if <condition>: pass
xxxxxxx
yyyyyyyy
zzzzzzzz
def sqrt(): pass
if True: pass pass
pass pass
if ..else
If block with else block is called if..else
This syntax define two blocks.
If block
Else block
Syntax:
if condition:
statement-1
STATEMENT-2
else:
statement-3
statement-4
statement-5
if ..else
If block with else block is called if..else
This syntax define two blocks.
If block
Else block
Syntax:
if condition:
statement-1
statement-2
else:
statement-3
statement-4
statement-5
If condition is True, it execute statement-1,statement-2 and
statement-5.
If condition is False, it execute statement-3, statement-4 and
statement-5.
# write a program to find given number is even or odd
num=int(input("Enter any number"))
if num%2==0:
print(num,"is even")
else:
print(num,"is odd")
# write a program to read name and age of person and find elg to
vote
name=input("Enter name")
age=int(input("Enter age"))
if age>=18:
print(name,"elg to vote")
else:
print(name,"not elg to vote")
if..elif..else (if..else ladder)
This syntax allows to check multiple conditions.
Syntax:
if <codition1>:
statement-1
statement-2
elif <condition2>:
statement-3
statement-4
elif <condition3>:
statement-5
statement-6
else:
statement-7
statement-8
If condition1 is True, it execute statement-1,statement-2 and
statement-8
If condition1 is False and Condtion2 is True, it execute statement-
3,statement-4 and statement-8
If condition1,condition2 are False and condition3 is True, it execute
statement=5,statement-6 and statement-8
If condition1,condition2,condition3 are false it execute statement-7
,statement-8
# write a program to find input number is +ve,-ve or zero
num=int(input("Enter any number"))
if num>0:
print(num,"is +ve")
elif num<0:
print(num,"is -ve")
else:
print("zero")
ord(),chr()
========
ord(),chr() are predefined functions which comes with
built-ins module.
ord(c)
Given a string representing one Unicode character, return an integer
representing the Unicode code point of that character.
>>> ord('A')
65
>>> ord('B')
66
>>> ord('a')
97
>>> ord('z')
122
>>> ord('0')
48
>>> ord('9')
57
chr(i)
Return the string representing a character whose Unicode code point
is the integer i.
>>> chr(97)
'a'
>>> chr(65)
'A'
>>> chr(48)
'0'
>>> chr(57)
'9'
# write a program to find input character is in uppercase or
lowercase
ch=input("enter any character") # a == 97
if ch>='A' and ch<='Z': # 65>=65 and 65<=90
print(ch,"is in uppercase")
elif ch>='a' and ch<='z': # 97>=97 and 97<=121
print(ch,"is in lowercase")
else:
print(ch,"is not alphabet")
# write a program to convert input alphabet in lowercase and
convert into uppercase
ch=input("enter any alphabet") # a
if ch>='a' and ch<='z':
x=ord(ch) # x=ord('a') ==> 97
x=x-32 # x=97-32 ==> 65
ch=chr(x) # chr(65) ==> A
print(ch)
elif ch>='A' and ch<='Z':
print(ch)
else:
print("input character is not alphabet")
ASCII Chart
A 65 a 97
B 66 b 98
C 67
D 68
E 69
….
Z 90
# convert input alphabet into uppercase or lowercase
ch=input("Enter any character")
if ch>='A' and ch<='Z':
x=ord(ch)
x=x+32
print(chr(x))
elif ch>='a' and ch<='z':
x=ord(ch)
x=x-32
print(chr(x))
else:
print("input character is not alphabet")
Nested if
If within if is called nested if or if followed by if is called nested.
if <condition1>:
if <condition2>:
statement-1
else:
statement-2
else
statement-3
statement-4
if condition1 is True and condition2 is True, it execute statement-1
if condition1 is True and condition2 is False, it execute statement-2
if condition1 is false statement-3
statement-4 is outside the if.
# login application
user=input("UserName")
pwd=input("Password")
if user=="nit":
if pwd=="nit123":
print("welcome to my application")
else:
print("invalid password")
else:
print("invalid username")
# banking application
accno=int(input("Enter Accno"))
bal=float(input("Enter Balance"))
ttype=input("Enter transaction type")
tamt=float(input("Enter transaction amount"))
if ttype=="withdraw":
if tamt<bal:
bal=bal-tamt
print("Transaction Completed")
else:
print("Insuff balance")
elif ttype=="deposit":
bal=bal+tamt
print("Transaction Completed")
else:
print("Invalid transaction Type")
print("Account ",accno)
print("Balance ",bal)
# finding max of 3 numbers without using and operator
num1=int(input("Enter first number"))
num2=int(input("Enter second number"))
num3=int(input("Enter third number"))
if num1>num2:
if num1>num3:
print(num1,"is max")
else:
print(num3,"is max")
elif num2>num3:
print(num2,"is max")
else:
print(num3,"is max")
Looping Statements or Iterative Statements
Looping statements are used to repeat one or more than one
statement number of times or until given condition.
Some times it is required to repeat the statements until some test
condition to solve given problem.
Python provide 2 types of looping statements
o While loop
o For loop
whie loop
While loop execute block of statements until given condition is True.
If condition is false it stop executing loop.
While loop is called entry controlled loop. The statements are
executed after checking condition.
Syntax:
while <condition>:
statement-1
statement-2
statement-3
Statement-1 and statement-2 are executed until given condition is
True. If condition is false it execute statement-3.
Inorder to work with while loop, we required 3 statements.
o Initialization statement
o Condition statement
o Updating statement
Initialization statement define the initial value of condition
Condition statement define how many times the loop has to repeated
Updating statement, which update the condition.
# write a program to print math table for input number
5x1=5
5x2=10
5x3=15
5x4=20
….
5x10=50
num=int(input("enter any number")) # 5
i=1
while i<=10:
p=num*i
print(num,"x",i,"=",p)
i+=1
# Write a program to print factorial of input number
# 4!=4x3x2x1=24
# 0!=1
num=int(input("Enter any number"))
fact=1
while num>=1:
fact=fact*num
num=num-1
print("Factorial is ",fact)
# write a program to find input number is prime or not
# num=5
# 5%1 5%2 5%3 5%4 5%5
# 1 2
num=int(input("enter any number"))
i=1
c=0
while i<=num:
if num%i==0:
c=c+1
i=i+1
if c==2:
print(num,"is prime")
else:
print(num,"is not prime")
# Write a program to find length of number of count of digits
# 6578 => 4 (count of digits)
num=int(input("Enter any number"))
count=0
num=abs(num)
while num!=0:
num=num//10
count=count+1
print(count)
n=int(input("enter n value"))
num=0
if n>=1 and n<=20:
while num<n:
print(num**2)
num=num+1
Write a program to print sum of digits of input number
num=int(input("Enter any number"))
s=0
while num>0:
r=num%10
s=s+r
num=num//10
print("Sum of digits ",s)
# write a program to find given number is pal or not
# write a program to find given number is armstrong number or not
# write a program to find given number is magic number or not
for loop or The for statement
The for statement is used to iterate over the elements of a sequence (such as
a string, tuple or list) or other iterable object:
Syntax:
for variable in sequence/iterable:
statement-1
statement-2
statement-3
for loop each time read one element/item from sequence or iterable and
execute statement-1,statement-2. This repeating is done until all the
elements read from sequence/iterable. After reading all the elements it
execute statement-3.
# write a program to count number of vowels in a given string
str1=input("Enter any string") # java
c=0
for ch in str1: # java
if ch in "aeiouAEIOU":
c=c+1
print("count of vowels are ",c)
# write a program to count number of alphabets,digits and
special character with in string
str1=input("Enter any string") # nit1*
acount=0
dcount=0
scount=0
for ch in str1: # nit1*
if ch>='A' and ch<='Z' or ch>='a' and ch<='z':
acount+=1
elif ch>='0' and ch<='9':
dcount+=1
else:
scount+=1
print("Alphabet count",acount)
print("Digit count ",dcount)
print("Special character count ",scount)
# write a program to convert string from lowercase to uppercase
str1=input("Enter any string") # abc
str2=""
for ch in str1: # abc ==> 97 98 99
if ch>='a' and ch<='z':
x=ord(ch)
x=x-32
str2=str2+chr(x)
else:
str2=str2+ch
print(str1)
print(str2)
# write a program to convert string from uppercase to lowercase
str1=input("Enter any string")
str2=""
for ch in str1:
if ch>='A' and ch<='Z':
x=ord(ch)+32
str2=str2+chr(x)
else:
str2=str2+ch
print(str1)
print(str2)
range
The range type represents an immutable sequence of numbers and is
commonly used for looping a specific number of times in for loops.
class range(stop)
class range(start, stop[, step])
The arguments to the range constructor must be integers (either built-in int
or any object that implements the __index__ special method). If the step
argument is omitted, it defaults to 1. If the start argument is omitted, it
defaults to 0. If step is zero, ValueError is raised.
For a positive step, the contents of a range r are determined by the formula
r[i] = start + step*i where i >= 0 and r[i] < stop.
For a negative step, the contents of the range are still determined by the
formula r[i] = start + step*i, but the constraints are i >= 0 and r[i] > stop.
range
The range type represents an immutable sequence of numbers and is
commonly used for looping a specific number of times in for loops.
class range(stop)
class range(start, stop[, step])
The arguments to the range constructor must be integers (either built-in int
or any object that implements the __index__ special method). If the step
argument is omitted, it defaults to 1. If the start argument is omitted, it
defaults to 0. If step is zero, ValueError is raised.
For a positive step, the contents of a range r are determined by the formula
r[i] = start + step*i where i >= 0 and r[i] < stop.
For a negative step, the contents of the range are still determined by the
formula r[i] = start + step*i, but the constraints are i >= 0 and r[i] > stop.
range(5) stop=5,start=0,step=1 0 1 2 3 4
r=range(5)
for value in r: # 0 1 2 3 4
print(value,end=' ')
print()
for value in range(10): # 0 1 2 3 4 5 6 7 8 9
print(value,end=' ')
print()
for value in range(int(5.8)): # 0 1 2 3 4
print(value,end=' ')
r1=range(1,10) # start=1,stop=10,step=1
for value in r1:
print(value,end=' ')
print()
r2=range(1,10,2) # start=1,stop=10,step=2
for value in r2:
print(value,end=' ')
print()
r3=range(10,0,-1)# start=10,stop=0,step=-1, if step -ve start>stop
for value in r3:
print(value,end=' ')
print()
r4=range(10,0,-2) # start=10,stop=0,step=-2, if step -ve start>stop
for value in r4:
print(value,end=' ')
print()
r5=range(1,10,-2) # strat=1,stop=10, if step -ve start>stop
for value in r5: # not generate any value
print(value,end=' ')
print()
r6=range(10,1,2) # start=10 stop=1 step=2 if step +ve start<stop
for value in r6: # not generate any values
print(value)
r1=range(-1,-11) # start=-1 stop=-11 step=1 if step +ve start<stop
for value in r1:
print(value,end=' ')
r2=range(-1,-11,-1) # start=-1 stop=-11 step=-1 if step -ve strat>stop
for value in r2:
print(value,end=' ')
print()
print()
r3=range(-11,0) # start=-11 stop=0 step=1 if step +ve start<stop
for value in r3:
print(value,end=' ')
r=range(1,10,0) # error because step should not be 0
for value in r:
print(value)
# write a range to generate alphabets from A-Z and a-z
r1=range(65,91)
r2=range(97,123)
for x in r1:
print(chr(x),end=' ')
print()
for x in r2:
print(chr(x),end=' ')
# write a program to generate values from 65 to 89
# without using numbers
for n in range(ord('A'),ord('Z')):
print(n)
# write a program to generate table for input number
num=int(input("enter any number"))
for i in range(1,11):
print(num,"*",i,"=",num*i)
# Write a program to find input number is prime or not
num=int(input("Enter number")) # 5
c=0
for i in range(1,num+1): # 1 2 3 4 5
if num%i==0: # 5%1 5%2 5%3 5%4 5%5
c=c+1
if c==2:
print(num,"is prime")
else:
print(num,"is not prime")
Q: What is difference between while loop and for loop?
While loop is used to execute block of statements until given condition is
True.
For loop is used to read elements/items from a sequence and execute block
of statements.
while <condition>: for variable in sequence/iterable:
statement-1 statement-1
statement-2 statement-2
# write a program to generate fibo series
#a b
# 0 1 1 2 3 5 8 ...n
# a b a+bc
a=0
b=1
n=int(input("enter the number of elements"))
print(a,b,end=' ')
for i in range(2,n+1):
c=a+b
print(c,end=' ')
a,b=b,c
Nested Loops
Loop inside a loop is called as nested loop.
Nested means within, if loop is defined inside loop it is called nested.
This nested loops can be,
Nested for loop
Nested while loop
Nested while loop
While loop inside while loop is called nested while loop.
Syntax
while <condition>: outer while loop
while <condition>: inner while loop
statement-1
statement-2
statement-3
While loop is executed until given condition is True.
# write a program to print all the prime numbers between 2 to n
n=int(input("enter the value of n"))
num=2
while num<=n:
i=1
c=0
while i<=num:
if num%i==0:
c=c+1
i=i+1
if c==2:
print(num)
num=num+1
Printing Pyramid Stars
n=int(input("enter how many rows"))
i=1
while i<=n:
k=0
j=1
while j<=n-i:
print(" ",end=" ")
j=j+1
while k!=2*i-1:
print("*",end=" ")
k=k+1
print()
i=i+1
#Printing Pyramid Stars
n=int(input("enter how many rows"))
i=n
while i>=1:
k=0
j=1
while j<=n-i:
print(" ",end=" ")
j=j+1
while k!=2*i-1:
print("*",end=" ")
k=k+1
print()
i=i-1
Branching statements or passes control statements
Break
Continue
branching statements are used to control the execution of looping
statements.
break statement is used to terminate execution of loop unconditionally.
continue statement is used to continue the execution of loop based on
some condition.
# write a program to print first 10 even numbers from 1 to 50
c=0
for num in range(1,51):
if num%2==0:
print(num)
c=c+1
if c==10:
break
c=0
# write a program to print first 10 odd numbers from 1 to 50
for num in range(1,51):
if num%2!=0:
print(num)
c=c+1
if c==10:
break
Write a program to print first 10 prime numbers from 1 to 100
Write a program to print first 10 prime numbers from 1 to 100
num=2
pcount=0
while num<=100:
i=1
c=0
while i<=num:
if num%i==0:
c=c+1
i=i+1
if c==2:
print(num,end=' ')
pcount=pcount+1
if pcount==10:
break
num=num+1
continue
Continue is keyword. This keyword is used inside looping statements.
It allows to move the execution control to the beginning of the loop.
for i in range(1,11): # 1 2 3 4 5 6 7 8 9 10
if i%2==0:
continue
print(i,end=' ')
after continue if any statement is given that statement is not executed.
For loop with else and while loop with else
Python allows to write for and while loop statement with else.
This else is executed after execution of while and for loop.
This else is not executed if the loop is terminated using break
statement.
while <condition>: for variable in <sequence>/<iterable>:
statement-1 statement-1
statement-2 statement-2
else: else:
statement-3 statement-3
# write a program to find factorial of given number
num=int(input("enter any number")) # 0
fact=1
while num>0:
fact=fact*num
num=num-1
else:
print(fact)
# using else with for loop
for i in range(1,11):
print(i)
else:
print("else block")
# write a program to print sum of first 10 even number from 1 to
n
n=int(input("enter how many values"))
esum=0
c=0
for num in range(1,n+1):
if num%2==0:
print(num,end=' ')
esum=esum+num
c=c+1
if c==10:
break
else:
print("with in given range less than 10 even numbers found")
if c==10:
print(esum)
formatting strings
Python allows to format the string in 3 ways
Old style formatting using %
New style formatting using format function
Formatting string using f-string
Old style formatting string using %
In python % is used to perform formatting operation on string.
If % is used with numbers it perform modulo operation.
If % is used with strings it execute format operation.
Using this %, we can format string the way how the formatting is
done using printf in C language.
Syntax:
“formatting fields”%(values separated with ,)
%d decimal integer
%o octal integer
%x hexadecimal integer
%b binary integer
%f float in fixed format
%e float in expo format
%s string
Example:
n1=10
n2=20
print("%d,%d"%(n1,n2))
print("sum of %d and %d is %d"%(n1,n2,n1+n2))
print("%d %d %d %d %d %d"%(10,20,30,40,50,60))
print("%d %d %d %d %d "%(10,20,10+20,10-20,10*20))
Write a program to print first 10 prime numbers from 1 to 100
num=2
pcount=0
while num<=100:
i=1
c=0
while i<=num:
if num%i==0:
c=c+1
i=i+1
if c==2:
print(num,end=' ')
pcount=pcount+1
if pcount==10:
break
num=num+1
continue
Continue is keyword. This keyword is used inside looping statements.
It allows to move the execution control to the beginning of the loop.
for i in range(1,11): # 1 2 3 4 5 6 7 8 9 10
if i%2==0:
continue
print(i,end=' ')
after continue if any statement is given that statement is not executed.
For loop with else and while loop with else
Python allows to write for and while loop statement with else.
This else is executed after execution of while and for loop.
This else is not executed if the loop is terminated using break
statement.
while <condition>: for variable in <sequence>/<iterable>:
statement-1 statement-1
statement-2 statement-2
else: else:
statement-3 statement-3
# write a program to find factorial of given number
num=int(input("enter any number")) # 0
fact=1
while num>0:
fact=fact*num
num=num-1
else:
print(fact)
# using else with for loop
for i in range(1,11):
print(i)
else:
print("else block")
# write a program to print sum of first 10 even number from 1 to
n
n=int(input("enter how many values"))
esum=0
c=0
for num in range(1,n+1):
if num%2==0:
print(num,end=' ')
esum=esum+num
c=c+1
if c==10:
break
else:
print("with in given range less than 10 even numbers found")
if c==10:
print(esum)
formatting strings
Python allows to format the string in 3 ways
Old style formatting using %
New style formatting using format function
Formatting string using f-string
Old style formatting string using %
In python % is used to perform formatting operation on string.
If % is used with numbers it perform modulo operation.
If % is used with strings it execute format operation.
Using this %, we can format string the way how the formatting is
done using printf in C language.
Syntax:
“formatting fields”%(values separated with ,)
%d decimal integer
%o octal integer
%x hexadecimal integer
%b binary integer
%f float in fixed format
%e float in expo format
%s string
Example:
n1=10
n2=20
print("%d,%d"%(n1,n2))
print("sum of %d and %d is %d"%(n1,n2,n1+n2))
print("%d %d %d %d %d %d"%(10,20,30,40,50,60))
print("%d %d %d %d %d "%(10,20,10+20,10-20,10*20))
x=65
print("x=%d,x=%o,x=%x"%(x,x,x))
# finding area of traingle
# input:
# enter base 1.0
# enter height 2.0
# output:
# area of triangle with base of 1.0 and height of 2.0 is ----
base=float(input("enter base"))
height=float(input("enter height"))
area=0.5*base*height
print("area of triangle with base of %f and height of %f is
%f"%(base,height,area))
print("area of triangle with base of %.2f and height of %.2f is
%.2f"%(base,height,area))
# write a program to input 2 values and perform arithmeric operation
# input:
# 100 200
#output:
# sum of 100 and 200 is 300
# diff of 100 and 200 is -100
# product of 100 and 200 is 20000
s=input("enter any two numbers separated with space")
x,y=s.split(" ")
n1=int(x)
n2=int(y)
print("sum of %d and %d is %d"%(n1,n2,n1+n2))
print("diff of %d and %d is %d"%(n1,n2,n1-n2))
print("product of %d and %d is %d"%(n1,n2,n1*n2))
New style formatting using format function
String provide one method called format.
Format method format the output or string by replacing fields.
Each field is defined with {}, this field contain index or name and
format.
These fields are identified with their positions, these positions starts
with 0.
“string”.format(list of values)
# each replacement field is identified with index or name
x=10
y=20
print("sum of {} and {} is {}".format(x,y,x+y))
print("sum of {1} and {0} is {2}".format(x,y,x+y))
#0 1 2
print("{2} is sum of {0} and {1}".format(x,y,x+y))
print("{a} and {b} is {c}".format(a=x,b=y,c=x+y))
print("{a} and {b} is {c}".format(c=x+y,a=x,b=y))
output:
sum of 10 and 20 is 30
sum of 20 and 10 is 30
30 is sum of 10 and 20
10 and 20 is 30
10 and 20 is 30
Syntax of format is
{fieldname/index:formatspecifier}
Format specifier consist of,
Fill,align,sign,width,groupting-options,type
Type:
The available integer presentation types are:
Type Meaning
'b' Binary format. Outputs the number in base 2.
Character. Converts the integer to the corresponding unicode
'c'
character before printing.
'd' Decimal Integer. Outputs the number in base 10.
'o' Octal format. Outputs the number in base 8.
Hex format. Outputs the number in base 16, using lower-case letters
'x'
for the digits above 9.
Hex format. Outputs the number in base 16, using upper-case letters
'X'
for the digits above 9.
Number. This is the same as 'd', except that it uses the current locale
'n'
setting to insert the appropriate number separator characters.
x=65
print("{:d},{:o},{:x},{:b},{:n}".format(x,x,x,x,x))
p=1234567
print("{:d}".format(p))
print("{:n}".format(p))
width define display width
n1=10
n2=100
n3=1000
n4=10000
print("{:6d}".format(n1))
print("{:6d}".format(n2))
print("{:6d}".format(n3))
print("{:6d}".format(n4))
The meaning of the various alignment options is as follows:
Option Meaning
Forces the field to be left-aligned within the available space (this is
'<'
the default for most objects).
Forces the field to be right-aligned within the available space (this
'>'
is the default for numbers).
'^' Forces the field to be centered within the available space.
n1=65
n2=456
n3=6578
n4=78956
print("{:6d}\n{:6d}\n{:6d}\n{:6d}".format(n1,n2,n3,n4))
print("{:<6d}{:<6d}{:<6d}{:<6d}".format(n1,n2,n3,n4))
print("{:^6d}\n{:^6d}\n{:^6d}\n{:^6d}".format(n1,n2,n3,n4))
The sign option is only valid for number types, and can be one of the
following:
Option Meaning
indicates that a sign should be used for both positive as well as
'+'
negative numbers.
indicates that a sign should be used only for negative numbers
'-'
(this is the default behavior).
indicates that a leading space should be used on positive numbers,
space
and a minus sign on negative numbers
a=+65
b=-85
print("{:+d}".format(a)) # +65
print("{:+d}".format(b)) # -85
print("{:-d}".format(a)) #65
print("{:-d}".format(b)) # -85
print("{: d}".format(a)) # 65
print("{: d}".format(b)) # -85
The available presentation types for floating point and decimal values are:
Type Meaning
Exponent notation. Prints the number in scientific notation using the
'e'
letter ‘e’ to indicate the exponent. The default precision is 6.
Exponent notation. Same as 'e' except it uses an upper case ‘E’ as the
'E'
separator character.
Fixed-point notation. Displays the number as a fixed-point number.
'f'
The default precision is 6.
Fixed-point notation. Same as 'f', but converts nan to NAN and inf to
'F'
INF.
a=1.567
print(a)
b=1567e-2
print(b)
print("{:f}".format(a))
print("{:e}".format(b))
n1=1.45
n2=12.2
n3=145.789
n4=1567.4568
print("{:10.2f}".format(n1))
print("{:10.2f}".format(n2))
print("{:10.2f}".format(n3))
print("{:10.2f}".format(n4))
The available string presentation types are:
Type Meaning
's' String format. This is the default type for strings and may be omitted.
name1="rajesh"
name2="kishore"
name3="james"
print("{:10s}".format(name1))
print("{:10s}".format(name2))
print("{:10s}".format(name3))
print("{:>10s}".format(name1))
print("{:>10s}".format(name2))
print("{:>10s}".format(name3))
print("{:^10s}".format(name1))
print("{:^10s}".format(name2))
print("{:^10s}".format(name3))
name1="rajesh"
name2="kishore"
name3="james"
print("{:*<10s}".format(name1))
print("{:*<10s}".format(name2))
print("{:*<10s}".format(name3))
print("{:*>10s}".format(name1))
print("{:*>10s}".format(name2))
print("{:*>10s}".format(name3))
print("{:*^10s}".format(name1))
print("{:*^10s}".format(name2))
print("{:*^10s}".format(name3))
f-string
Format string is introduced in python 3.8 version
Format string is prefix with f'string'
Format string contain replacement fields and each field is
represented using curly braces {}
This field is identified with variable name/expression.
This field is replace with value of variable
This field can also using type characters
a=10
b=20
c=30
print(f'{a},{b},{c}')
x=1.5
y=1.25
print(f'{x},{y}')
print(f'sum of {a} and {b} is {a+b}')
print(f'{a:d} {b:o} {c:x}')
print(f'{x:5.2f},{y:5.2f}')