Skip to content

7.6 String formatting (again)

Conversion specifiers

  • A string formatting expression allows a programmer to create a string with placeholders that are replaced by the values of variables
  • Such a placeholder is called a conversion specifier, and different conversion specifiers are used to perform a conversion of the given variable value to a different type when creating the string.
  • The syntax for using a conversion specifier also includes a % symbol between the string and the value or variable to be placed into the string
    • Ex: print('The couch is %d years old.' % couch_age)

7%206%20String%20formatting%20(again)%20e018b6435f64487e9e2511d47619c4d1/Untitled.png

Multiple Conversion Specifiers

  • Multiple conversion specifiers can appear within the string formatting expression.
  • Expressions that contain more than one conversion specifier must specify the values within a tuple following the '%' character.

7%206%20String%20formatting%20(again)%20e018b6435f64487e9e2511d47619c4d1/Untitled%201.png

-