Simple Print Variations – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #PYTHON#6732    siteicon   siteicon  

Simple Print Variations

Some simple variations to print() function

Learning Objectives

  • Bringing some content to standard output.
  • Understanding multiple parameters in print.
  • Understanding to print multiple prints in one line.
  • Understanding simple string concatenation within print() function argument.

Source Code

TC++ #6732

Source Code

Run Output

Hello World
Hello World
HelloWorld
Hello World

Code Understanding

print(“Hello World”)
This is the standard way of outputting to screen.

print(“Hello”,”World”)
This way two parameters with string literal values are being printed with a space in between

print(“Hello”+”World”)
This way two string literals are being first concatenated and then it is being printed. Since no space is given in any literal so both strings are joined without a space.

print(“Hello”,end=” “); print(“World”)
These are two print instructions one after the another. Two instructions in the same line can be given with a semicolon in between. First instruction also has a end parameter as space. by default without this parameter print instruction always adds a newline character. Here it adds a space and not newline character. So two string literals are printed with a space in between.

Notes

Common Errors

  • Some time students write print in old style (ver 2.x style – without function brackets) and try to interpret it in version 3.x of python then error is bound to come.


Share

CSKC| Created: 12-Feb-2019 | Updated: 23-Feb-2019|









Back