Перейти до змісту

Printing 🖨️ & Formatting 📝

In this section, we will figure out in the printing to stdout and stderr using built-in functions / macros.

Printing 🖨️

Standard Output (stdout)

Python uses the built-in function print() to write output to stdout (Docs).

To remove or change space between objects to print you can provide sep keyword argument.

To remove or change new line at the end, you can use end keyword argument.

if __name__ == "__main__":
    value_1 = "Hello"
    value_2 = "World"

    # Simple write to stdout,
    # with new line, and space between printed objects.
    print(value_1, value_2)

    # Write to stdout (change space between objects to "_").
    print(value_1, value_2, sep="_")

    # Simple write to stdout (without new line)
    print(value_1, value_2, end="")

Rust uses build-in macros println!() to write output to stdout (Docs).

To remove or change space between objects to print should do it manually.

To remove new line at the end, you should use another built-in macro print! (Docs).

fn main() {
    // Rust's compiler automatically determined `&str` type,
    // so no need to declare it.
    let value_1 = "Hello";
    let value_2 = "World";

    // Simple write to stdout,
    // with new line, and space between printed objects.
    println!("{} {}", value_1, value_2);
    // Write to stdout (change space between objects to "_").
    println!("{}_{}", value_1, value_2);
    // Simple write to stdout (without new line).
    print!("{} {}", value_1, value_2);
}

Output will be:

Hello World<NEW_LINE>
Hello_World<NEW_LINE>
Hello World

Standard Error (stderr)

To print something in stderr you can modify file keyword argument to sys.stderr.

To remove or change space between objects to print you can provide sep keyword argument.

To remove or change new line at the end, you can use end keyword argument.

1
2
3
4
5
6
7
8
9
import sys

if __name__ == "__main__":
    value_1 = "Hello"
    value_2 = "World"

    print(value_1, value_2, file=sys.stderr)
    print(value_1, value_2, sep="_", file=sys.stderr)
    print(value_1, value_2, end="", file=sys.stderr)

To print something in stderr you should use another built-in macro eprintln! (Docs).

To remove or change space between objects to print should do it manually.

To remove new line at the end, you should use another built-in macro eprint! (Docs).

1
2
3
4
5
6
7
8
fn main() {
    let value_1 = "Hello";
    let value_2 = "World";

    eprintln!("{} {}", value_1, value_2);
    eprintln!("{}_{}", value_1, value_2);
    eprint!("{} {}", value_1, value_2);
}

Output will be:

Hello World<NEW_LINE>
Hello_World<NEW_LINE>
Hello World

Formatting 📝

In latest Python version the best approach to format strings and data types is f-string or formatted string literal (declared with f"{}") (f-string docs, .format() docs).

Basic str interpolation
if __name__ == "__main__":
    value_1 = "Hello"
    value_2 = "World"

    # Use <obj.>__str__() or str(<obj>)
    print(f"{value_1} {value_2}")  # default with f-string
    print("{0} {1}".format(value_1, value_2))  # with .format() by indexes
    print("{hello} {world}".format(hello=value_1, world=value_2))  # with .format() by names
    print(f"{value_1=} {value_2=}")  # with names
    # Use <obj>.__str__() or str(<obj>)
    print(f"{value_1!s} {value_2!s}")
    # Use <obj>.__repr__() or repr(<obj>)
    print(f"{value_1!r} {value_2!r}")
    # Use ascii(<obj>)
    print(f"{value_1!a} {value_2!a}")
Output
Hello World
Hello World
Hello World
value_1='Hello' value_2='World'
Hello World
'Hello' 'World'
'Hello' 'World'

In Rust standard approach to format string and data types is std::fmt or format! macro (declared "{}").

Basic str interpolation
fn main() {
    let value_1 = "Hello";
    let value_2 = "World";

    // use Display trait
    println!("{} {}", value_1, value_2); // default by placeholders
    println!("{0} {1}", value_1, value_2); // by indexes
    println!("{hello} {world}", hello = value_1, world = value_2); // by names
    println!("value_1='{}' value_2='{}'", value_1, value_2); // custom names manually

    // use Debug trait
    println!("{:?} {:?}", value_1, value_2);
    // use Debug and pretty-print
    println!("{:#?} {:#?}", value_1, value_2);
}
Output
Hello World
Hello World
Hello World
value_1='Hello' value_2='World'
"Hello" "World"
"Hello" "World"