User Program Communication

Print in Python

Print is used to output text to the console.


Syntax
#print content, with variable substitution
print ("content {}".format(variable))

#print combination of variables and strings, single space added automatically
print ("content", variable, "other content")

Notes

In Python 3, print is a function, and the string is passed as a function parameter, so they must be surrounded by brackets.

Format allows you to add formatting inside the curly braces, to format the substituted variable a certain way.

If specific formatting is used (specifier), the curly brackets are optional.

Specific formatting notes:
%f (floating-point), %e or %E (floating-point in scientific notation), %g or %G (uses the shorter version of %f or %e / %E), and the string name for strings.
For floating point numbers, precision can be indicated (the number of decimal points wanted) by adding: %.3f, where 3 represents the number of decimal points.

To pad a number with zeros, add %03f, where 3 indicates the total number of values.

Multiple specifiers can be used in this order: %[flag][width][.][precision][conversion specifier]. The flag can be: #, 0 (padding), -, + (shifting). The precision determines the maximum number placements where the width determines the minimum number placements. The conversion specifier indicates the variable type of the output.


Example
name = "Guido"
print ("Hello, {}".format(name)) #will output "Hello, Guido"

< Generators   |   User Input >

© 2019 SyntaxDB. All Rights Reserved.