Output Statements
Output statements displays textual output on the computer screen. We can use print() function to display output.
Form 1: print() without any argument
Just it prints new line character.
Form-2
print(String) print("Waytoeasylearn") We can use escape characters also print("Waytoeasy \n learn") print("Waytoeasy\tlearn") We can use repetetion operator (*) in the string print(10*"Waytoeasylearn") print("Waytoeasylearn"*10) We can use + operator also print("Waytoeasy"+"learn")
Note
- If both arguments are String type then + operator acts as concatenation operator.
- If one argument is string type and second is any other type like int then we will get Error.
- If both arguments are number type then + operator acts as arithmetic addition operator.
print("Hello"+"World") print("Hello","World") HelloWorld Hello World
Form-3: print() with variable number of arguments
a,b,c=10,20,30 print("The Values are :",a,b,c) The Values are : 10 20 30
By default output values are separated by space. If we want we can specify separator by using “sep” attribute.
a,b,c=10,20,30 print(a,b,c,sep=',') print(a,b,c,sep=':') 10,20,30 10:20:30
If we want output in the same line with space
print("Hello",end=' ') print("Welcome to ",end=' ') print("Waytoeasylearn") Hello Welcome to Waytoeasylearn
Note
The default value for end attribute is \n, which is nothing but new line character.
Form-4: print(object) statement
We can pass any object (like list, tuple, set etc) as argument to the print() statement.
l=[10,20,30,40] t=(10,20,30,40) print(l) print(t)
Form-5: print(String, variable list)
We can use print() statement with String and any number of arguments.
s = "Waytoeasylearn" s1 ="Java" s2 ="Python" print("Hello",s) print("You are posting",s1,"and",s2," tutorials") Hello Waytoeasylearn You are posting java and Python tutorials
Form-6: print (formatted string)
- %i ==> int
- %d ==> int
- %f ==> float
- %s ==> String type
Syntax
print("formatted string" %(variable list))
E.g
a=10 b=20 c=30 print("a value is %i" %a) print("b value is %d and c value is %d" %(b,c)) a value is 10 b value is 20 and c value is 30
Form-7: print() with replacement operator {}
name = "Ashok Kumar" salary = 50000 friend = "Santosh" print("Hello {0} your salary is {1} and Your Friend {2} is waiting". format(name,salary,friend)) print("Hello {x} your salary is {y} and Your Friend {z} is waiting". format(x=name,y=salary,z=friend)) Hello Ashok Kumar your salary is 50000 and Your Friend Santosh is waiting Hello Ashok Kumar your salary is 50000 and Your Friend Santosh is waiting