InterviewSolution
| 1. |
The built-in print() function accepts a number of optional arguments. Explain their use with an example. |
|
Answer» Obviously, print() is most commonly used Python statements and is used to display the value of one or more objects. However, it accepts other arguments other than objects to be displayed. print(*objects, sep=' ', end='\n', file=sys.stdout, FLUSH=False)The function can have any number of objects as positional arguments, all separated by commas. Other arguments are optional but must be used as KEYWORD arguments. By default, the non-keyword arguments that is the list of objects is displayed with WHITESPACE as a separator. However, you can define separator symbol with the help of sep argument. >>> x=10 >>> y=20 >>> z=30 >>> print (x,y,z) 10 20 30 >>> #using comma as separator >>> print (x,y,z, sep=',') 10,20,30Second keyword argument controls the end of line character. By default output of each print() statement terminates with newline character or ‘\n’. If you wish you can change it to any desired character. print ("Hello World") print ("Python programming") #this will cause output of next statement in same line print ("Hello World", end=' ') print ("Python programming") output: Hello World Python programming Hello World Python programmingThe file argument decides the output stream to which result of print() is directed. By default it is sys.stdout which is standard output
The buffer argument is false by default, but if the flush keyword argument is true, the stream is forcibly flushed. |
|