- We can use print() function to display output.
- The following forms of print we use in python …
print(): Without any argument
- Prints new line character.
print(var): With an argument
- Print var in new line.
Example
Python
# print(string)
print("Hello World")
# We can use escape characters also
print("Hello \n World")
print("Hello\tWorld")
# We can use repetetion operator *
print(10*"Hello")
print("Hello"*10)
# We can use + operator also
print("Hello"+"World")
# We can use , operator also
print("Hello","World")
Output
PowerShell
Hello World
Hello
World
Hello World
HelloHelloHelloHelloHelloHelloHelloHelloHelloHello
HelloHelloHelloHelloHelloHelloHelloHelloHelloHello
HelloWorld
Hello World
Remark
- 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(var1, var2, var3 …): With multiple arguments
- Print all variables in a line with separator space.
Example
Python
num1, num2, num3 = 10, 20, 30
print("The Values are :",a, b, c)
Output
PowerShell
OutputThe Values are : 10 20 30
- By default output values are separated by space. If we want we can specify separator by using “sep” attribute.
Example
Python
num1, num2, num3 = 10, 20, 30
print(num1, num2, num3, sep=',')
print(num1, num2, num3, sep=':')
Output
PowerShell
10,20,30
10:20:30
print(end=” “): With end attribute
- The default value for end attribute is \n, which is nothing but new line character.
- If we want output in the same line with space then use end=” “
Example
Python
print("Hello", end=' ')
print("Ajay", end=' ')
print("Shukla")
Output
PowerShell
Hello Ajay Shukla
print(object): With object
- We can pass any object (like list, tuple, set … etc.)as argument to the print() statement.
Example
Python
l=[10,20,30,40]
t=(10,20,30,40)
print(l)
print(t)
Output
PowerShell
[10, 20, 30, 40]
(10, 20, 30, 40)
print(String, variable)
- We can use print() statement with String and any number of arguments.
Example
Python
s="Ajay"
a=28
s1="Data Science"
print("Hello",s,"Your Age is",a)
print("You are teaching",s1)
Output
PowerShell
Hello Ajay Your Age is 28
You are teaching Data Science
print(formatted string)
- Syntax: print(“formatted string” %(variable list))
- Used representation …
- %i====>int
- %d====>int
- %f=====>float
- %s======>String type
Example
Python
a=10
b=20
c=30
print("a value is %i" %a)
print("b value is %d and c value is %d" %(b,c))
Output
PowerShell
a value is 10
b value is 20 and c value is 30
Example
Python
s="Ajay"
list=[10,20,30,40]
print("Hello %s …The List of Items are %s" %(s,list))
Output
PowerShell
Hello Ajay …The List of Items are [10, 20, 30, 40]
print(): With replacement operator {}
Example
Python
name="Ajay"
salary=10000
friend="Amit"
print("Hello {0} your salary is {1} and Your Friend {2} is waiting".format(name,salary,friend))
Output
PowerShell
Hello Ajay your salary is 10000 and Your Friend Amit is waiting
Ungraded Questions
Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!