본문 바로가기
Python(Eng. ver)

1. Python Output

by 곽정우 2024. 3. 12.

1. Print Function

The print() function is the most basic output function in Python. It is used to display text or data on the terminal or console window.

  • sep : Specifies the separator to be inserted between values. The default value is a space (' ').
  • end : Specifies the character to be printed at the end of all values. The default value is a newline ('\n').

 

2. Escape Characters in Python

Escape characters in Python are used to represent characters that have special meaning within a string. Escape characters start with a backslash () and are used to represent specific actions or special characters within a string.

  • \n: Indicates a new line.
  • \t: Represents a tab character.
  • \\: Represents a backslash itself.
  • \" or \': Represents a double or single quote, respectively. Used to differentiate from the quotes surrounding the string.
  • \r: Represents a carriage return. Moves the cursor to the beginning of the line after printing the string.
  • \b: Represents a backspace. Deletes one character from within the string.

 

3. Arithmetic Operations within the print() Function

The print() function can print not only strings but also numbers and the results of operations.

 

4. Output Formatting

Python's print() function provides the ability to format and print values using format specifiers. The % operator and the format() method are used with format specifiers to achieve the desired output format.

 

4-1. Using the % Operator for Output Formatting

  • %s: Prints a string.
  • %d: Prints an integer.
  • %.nf: Prints up to n decimal places.

4-2. Using the format() Method for Output Formatting

  • {}: Place variables or expressions inside curly braces to print their values.
  • {:d}: Prints an integer.
  • {:f}: Prints a floating-point number.
  • {:s}: Prints a string.
  • {:x}: Prints an integer in hexadecimal.
  •  
  • {:n.mf}: Prints a floating-point number with total length n and m decimal places.

 

5. Comments

Python comments are used to include programmer-written explanations or notes within the code. Comments help to understand the code and collaborate with other developers, and can clearly explain the functionality and purpose of the code. Comments are ignored during program execution and therefore do not affect the execution of the code.

 

5-1. Single-line Comment

  • To comment on a single line of code. The # symbol is used to start a comment. Everything written after # on that line is treated as a comment.

5-2. Multi-line Comment

  • To comment on multiple lines of code. A multi-line comment is written between three double quotes (""") or three single quotes (''').

'Python(Eng. ver)' 카테고리의 다른 글

6. Python collection type - Set  (0) 2024.03.14
5. Python collection type - Tuple  (0) 2024.03.14
4. Python collection type - List  (0) 2024.03.14
3. Python String & why 0.1+ 1.1 ≠ 1.2  (0) 2024.03.13
2. Variables in Python  (0) 2024.03.13