19. Output Statements

 


Output Statements

            The Output Statement is use for, if we want to provide any output statement then we need the Print() statement. in this print() statement we have some methods also there. 

1st Method:


Print() without any agreement and the result will be a new line created.

Ex1:

Print()

a new line was created...# but not showing anything. we take one more example.

Ex2:

print("krishna")
print("")
print("devi")

Result:

krishna


devi   

2nd Method:


Print() without some agreement like any agreement or any escape characters we can use.

Ex1:

print("krishna")

Result:

krishna

Ex2:

print("krishna\ngopi\travi")

Result:

krishna

gopi  ravi

3rd Method:


in this method, we can assign multiple assignments with multiple values.

Ex:

a,b,c,d = 10,20,30,40

print(a,b,c,d)

Result:

10 20 30 40 # result with spaces

now going to focus on some attributes 

above example if we take a,b,c,d = 10,20,30,40 and we print(), then result we will be 10 20 30 40, in this result we adsorb that result will be space in-between numbers. but we don't want to be space in between results. We want to do something else on the final result. then in this type of scenario sep attribute will help.

Ex: 

a,b,c,d = 10,20,30,40

print(a,b,c,d,sep=':')

Result:

10:20:30:40 # result with colon

now we use one more attribute called end

if we use the end attribute then it will add attribute characters within the same line.

Ex:

print("Krishna")
print("ravi")
print("gopi")

Result:

Krishna 

ravi

gopi

Ex:

print("Krishna"end="")
print("ravi"end="")
print("gopi"end="")

Result:

Krishnaravigopi

Ex:

print("Krishna"end=",")
print("ravi"end=",")
print("gopi"end=",")

Result:

Krishna,ravi,gopi,

now just one more example with sep and end attributes.

Ex:

print(10,20,30,sep =':'end="***")
print(40,50,60sep = ':')
print(70,80sep="@@"end'$$')

Result:

10:20:30***40:50:60

70@@80$$


some more examples on output statements

Ex:

print('krishna''gopi')
print('krishna','gopi')

Result:

krishnagopi

krishna gopi

Print statement take any type of object will take without any error

EX:

object_one = [10,20,30,40]
object_two = (10,20,30,40)
object_three = {10,20,30,40}
object_four = ([10,20,30,40])
print(object_one,object_two,object_three,object_four)

Result:

[10, 20, 30, 40] (10, 20, 30, 40) {40, 10, 20, 30} [10, 20, 30, 40]


print() statement with replacement operator {}

Ex:

name = ("krishna")
amount = ("10,000")
company = ("sis bank")


print('Hi, {} you have taken {} on this {}.' .format(
    nameamountcompany))

Result:

Hi, krishna you have taken 10,000 on this sis bank.

Ex:

one more option in index type

name = ("krishna")
amount = ("10,000")
company = ("sis bank")


print('Hi, {0} you have taken {1} on this {2}.' .format(
    nameamountcompany))

Result:

Hi, krishna you have taken 10,000 on this sis bank.

Ex:

one more option in keyword type

name = ("krishna")
amount = ("10,000")
company = ("sis bank")


print('Hi, {n} you have taken {a} on this {c}.' .format(
    n=namea=amountc=company))

Result:

Hi, krishna you have taken 10,000 on this sis bank.

but the almost all-time first one is mostly used and recommended.

print() statement with formatted string

%i = signed decimal value

%d = signed decimal value

%f = signed float value

%s= signed string value

Ex:

a = 10
print('a value = %i' % a)

a value = 10    #Result

Ex:

a = 10
b = 20
c = 30

print('a value = %d,b value = %d,c value = %d' % (a, b, c))

a value = 10,b value = 20,c value = 30    #Result

Ex:

name = 'krishna'
marks = [60, 72, 86, 42, 79]
print('hello %s your marks  %s' % (name, marks))

hello krishna your marks  [60, 72, 86, 42, 79] #Result

replacement operator {} and formatted string are working both same looks like. but some advantage of formatted string checks on below 

Ex:

number = 789.2345

print('number value ={}'.format(number))
print('number value =%f' % number)

number value =789.2345    #Result

number value =789.234500

but if we add like below on 2nd line

print('number value =%.2f' % number)

the result will be number value =789.23

so this is the advantage. we can collect an accurate number as per our requirement.


0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post