object | To be printed to screen or file |
sep | Optional , separator can be used between the outputs |
end | Optional, default is '\n', used to remove line break after the output |
file | Optional , file object to write to file |
flush | Optional , Default value is False, Managing buffer |
print('Hello world')id=5name="Ronal Jack"mark=45print("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=5print(" 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 name='My Name'age=5print("Name {:>25s}, Age {:2d}".format(name,age))Now the name variable will align right ( within the 25 char space ) Name My Name, Age 5my_str="Mr ABCD"avg=356.23456id=5name="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='' within the print ())
my_str="Mr ABCD"avg=356.23456id=5name="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...print('welcome','to','plus2net', sep='-')Outputwelcome-to-plus2netprint('Welcome '+ 'to '+'plus2net')OutputWelcome to plus2netflash=True we can send the outputs in steps with a delay of 1 second. import timefor i in range(5,0,-1): print(i,end=' >> ',flush=False) # default value for flush #print(i,end=' >> ',flush=True) # delay in each output time.sleep(1) # delay of 1 second print('Over')
print('Your visitor number is :' + 34)We can use str() to change our integer to string. print('Your visitor number is :' + str(34))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. a=2b="welcome"c="Python"print("Hello ",b," to ", c )Output is here Hello welcome to Python
Let us print I can't travel by busprint(' 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. print(r"C:\MyDocument\python\test.py")Here \t is not considered as Tab and the out put is here C:\MyDocument\python\test.pyprint ( 'We said "Welcome to plus2net"')Output is here We said "Welcome to plus2net"my_list=['One','Two','Three'] # A list print(my_list) # ['One', 'Two', 'Three']my_list=('One','Two','Three') # A tuple print(my_list) # ('One', 'Two', 'Three')my_set={'Alex','Ronald','John'}print(my_set)my_dict={1:'Alex',2:'Ronald'}print(my_dict) # {1:'Alex',2:'Ronald'}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.