{ }codelper

Learn › Python

Basics · 01

Printing to the screen

Your first line of code: print text and the result of a calculation.

The first step in programming is output: writing something on the screen (standard output).
The example below prints one line of text and one calculated result.

print("Hello, World!")print(3 + 4)
stdout
Hello, World!
7

Key points

  • Text goes in quotes.
    Put a calculation in quotes, like "3 + 4", and it is printed as plain characters, not calculated.
  • A calculation written without quotes prints its result.
    3 + 4 prints 7.
  • Most print functions end the line for you.
    There is usually a separate one that keeps writing on the same line.
  • Codelper's judge compares your output with the answer character by character.
    One extra space or missing newline makes it wrong.
  • Some languages run straight from the first line of the file; others start in a fixed main function.

How each language writes it

Pythonprint(...) — stay on the line with print(x, end="").
Runs from the first line of the file
JavaScriptconsole.log(...) — stay on the line with process.stdout.write(...).
Runs from the first line of the file
TypeScriptSame as JavaScript
JavaSystem.out.println(...) — stay on the line with System.out.print(...).
All code lives in a main function inside a class
C#Console.WriteLine(...) — stay on the line with Console.Write(...).
As in the example, it starts in a Main function inside a class
C++std::cout << ... << '\n' — never ends the line by itself, so write '\n' yourself.
Starts at main
Cprintf("...\n") — write the \n yourself.
puts(...) adds the newline for you.
Starts at main
CraykonConsole.WriteLine(...) — stay on the line with Console.Write(...).
Runs from the first line of the file

Common mistakes

Joining numbers onto text with +Java · JavaScript · TypeScript · C# · Craykon

console.log("Sum: " + 3 + 4);

→ Sum: 34 — left to right, 3 is glued onto the text, then 4 onto that

Group the math first: "Sum: " + (3 + 4).
In Python, joining text and a number with + is a TypeError.

Forgetting \nC · C++

printf("Hello, World!");
printf("%d", 3 + 4);

→ Hello, World!7 — the two lines run together, so the answer is wrong

End every line with \n: printf("%d\n", 3 + 4);

How languages differ
print, console.log, println, WriteLine and Craykon's Console.WriteLine end the line for you.
C's printf and C++'s cout do not, so write \n yourself.

Practice