Home > Articles > Open Source > Python

  • Print
  • + Share This
This chapter is from the book


String formatting is the way we organize instructions so that Python can understand how to incorporate data in the creation of strings. How strings are formatted determines the presentation of this data. The basis of string formatting is its use of the formatting directives.

Logical operators (and, or, not) return true or false values. Comparison operators (in, not in, is, is not, ==, !=, <>, >, >=, <, <=) compare two values—is one value greater than another, or is one value not equal to another? Comparisons always return 1 for true and 0 for false.

Any value in Python equates to a Boolean true or false. A false can be equal to none, a zero of numeric type (0, 0l, 0.0), an empty sequence ('', (), []), or an empty dictionary ({}). All other values are considered true.

Sequences, tuples, lists, and strings can be added or multiplied by a numeric type with the addition and multiplication operators (+, *), respectively. Strings can be formatted with tuples and dictionaries using the format directives %i, %d, %f, and %e. Formatting flags can be used with these directives.

  • + Share This
  • 🔖 Save To Your Account