logo

Programming Languages - Print

Last Updated: 2024-01-21

Go

import "fmt"

// without new line
fmt.Print()

// with new line
fmt.Println()

// with format
fmt.Printf("Foo = %d", foo)

// print to file
fmt.Fprint(os.Stdout, "Hello ", 23, "\n")

// print to string
s := fmt.Sprintf("%s: %s\n", v1, v2)

format:

  • %v: (v for value) catchall (use the default conversion). Can print any value, even arrays, slices, structs, and maps.
  • %+v: prints struct field names, e.g. &{a:7 b:-2.35 c:abc def} (%v only prints values like &{7 -2.35 abc def})
  • %#v: prints the value in full Go syntax, e.g. &main.T{a:7, b:-2.35, c:"abc\tdef"}
  • %q: prints quoted string when applied to a value of type string or []byte
  • %#q: use backquote instead.
  • %x: prints hexadecimal strings.
  • % x: similar to %x but puts spaces between the bytes.
  • %T: prints the type of a value.

Python

# with new line
print("1")

# without new line
print("1", end='')

pprint can pretty-print arbitrary Python data structures

>>> import pprint

>>> pp = pprint.PrettyPrinter(indent=4)
>>> pp.pprint(foo)

Java

// with new line
System.out.println()

// without new line
System.out.print()

// print array
System.out.println(Arrays.toString(arr));