print()

print(object ,sep='Separator',end='end',file=file,flush=flush) returns output to screen or file
objectTo be printed to screen or file
sepOptional , separator can be used between the outputs
endOptional, default is '\n', used to remove line break after the output
fileOptional , file object to write to file
flushOptional , Managing buffer

String output

print('Hello world')

  • Video Tutorial on print()


printing output with format

id=5
name="Ronal Jack"
mark=45
print("ID :{:02d} Name:{:15s} Mark: {:2d}".format(id,name,mark))
Output is here
ID :05 Name:Ronal Jack      Mark: 45
More on placeholder {}
str1="Welcome "
str2=" to "
str3=" Python "
str4=" Your student id = "
id=5

print(" Hi {2} {1} {0} ".format(str1,str2,str3))
print(" Hi {0} {1} ".format(str4,id))
Output is here
Hi  Python   to  Welcome  
 Hi  Your student id =  5 
Correct the first line to get proper sequnce of words

Right align

name='My Name'
age=5
print("Name  {:>25s}, Age {:2d}".format(name,age))
Now the name variable will align right ( within the 25 char space )
Name                    My Name, Age  5

Printing decimal upto 2 places using format

my_str="Mr ABCD"
avg=356.23456
id=5
name="Welcome to Python"

print("Hi {}, Your id = {}, Your Average ={:.2f})".format(my_str,id,avg))
Output is here
Hi Mr ABCD, Your id = 5, Your Average =356.23)

end

Python adds one line break ( default value of end='\n' ) after each print command, we can remove the line break ( Watch the end='' within the print ())
my_str="Mr ABCD"
avg=356.23456
id=5
name="Welcome to Python"

print("Hi {}, Your id = {} ".format(my_str,id),end='')
print("Your Average ={:.2f})".format(avg))
output is here
Hi Mr ABCD, Your id = 5 Your Average =356.23)
We can use end to print any string at the end of the output. Check here how we used end='...' to print dots after the number.
for i in range (4):
  print(i,end='...')
Output
0...1...2...3...

sep

We can add a separator in between strings
print('welcome','to','plus2net', sep='-')
Output
welcome-to-plus2net

Multiple strings with +

print('Welcome '+ 'to '+'plus2net')
Output
Welcome to plus2net

String and Integer

We can't print string with integer, this will generate error. TypeError: must be str, not int
print('Your visitor number is :' + 34)
We can use str() to change our integer to string.
print('Your visitor number is :' + str(34))

Write to file

with open('my_file.txt', mode='w') as fob:
    print('hello world', file=fob)
This will create my_file.txt file in same directory and write 'hello world' to it.

Printing using variables inside string

a=2
b="welcome"
c="Python"
print("Hello ",b," to ", c )
Output is here
Hello  welcome  to  Python

Using escape char

Test on Basics of Python Let us print I can't travel by bus
print(' I can\'t travel by bus')
We used escape char \ inside the word can't to print single quote instead of using as string termination.
We can use special char \ inside our string without using as escape char by adding r before the quotes
print(r"C:\MyDocument\python\test.py")
Here \t is not considered as Tab and the out put is here
C:\MyDocument\python\test.py

We can add doble quote inside single quote
print ( 'We said "Welcome to plus2net"')
Output is here
We said "Welcome to plus2net"

print() with list

More on List
my_list=['One','Two','Three'] # A list 
print(my_list) # ['One', 'Two', 'Three']

print() with tuple

More on tuple
my_list=('One','Two','Three') # A tuple 
print(my_list) # ('One', 'Two', 'Three')

print() with set

more on set
my_set={'Alex','Ronald','John'}
print(my_set)

print() with dictionary

More on Dictionary
my_dict={1:'Alex',2:'Ronald'}
print(my_dict) # {1:'Alex',2:'Ronald'}

All Built in Functions in Python variables in Python complex()

View and Download print_v1 ipynb file ( .html format )


plus2net.com



Post your comments , suggestion , error , requirements etc here





Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer