String Formatting

In this class, we will learn about string formatting, which is the process of formatting character strings so that they are displayed in a specific way.

Basic formatting with .format() Link to heading

The format() method of character strings allows you to insert variables into the string and format its output.

Example Link to heading

name = "Anna"
age = 25

# Basic formatting
greeting = "Hello, {}! You are {} years old.".format(name, age)

print(greeting) # Hello, Ana! You have 25 years.

In this example:

  • the format() method is used to insert the variables name and age into the string.
  • Variables are inserted between braces {}.
  • The order of the variables inside the curly braces must match the order of the arguments to the format() method.

Formatting options with .format() Link to heading

The format() method allows you to specify formatting options for variables.

Example Link to heading

name = "Anna"
age = 25

# Formatting with options
greeting = "Hello, {name}! You are {age:2d} years old.".format(name=name, age=age)

print(greeting) # Hello, Ana! You have 25 years.

In this example:

  • The :2d option is used to format the age variable as a two-digit integer.
  • The :s option can be used to format the variable as a character string.
  • The :f option can be used to format the variable as a floating point number.

Other formatting options with .format() Link to heading

  • align: Text alignment (<, ^, >)
  • width: Width of the field
  • fill: Fill character
  • precision: Precision for floating point numbers

Example Link to heading

name = "Anna"
age = 25.5

# Formatting with various options
greeting = "Hello, {name:-^10}! You are {age:6.2f} years old.".format(name=name, age=age)

print(greeting) # Hello, -----Ana-----! You are 25.50 years old.

In this example:

  • The :-^10 option is used to left-align the name with a width of 10 characters and pad it with hyphens.
  • The option :6.2f is used to format the age as a floating point number with 6 digits total and 2 decimal places.

Formatting f-strings Link to heading

f-strings are a modern way to format character strings in Python. They allow variables to be interpolated directly within the chain.

Example Link to heading

name = "Anna"
age = 25

# Formatting with f-strings
greeting = f"Hello, {name}! You are {age} years old."

print(greeting) # Hello, Ana! You have 25 years.

Another way of basic formatting Link to heading

Character strings can be formatted using the % operator. This operator allows you to specify the format of the string using a series of format codes.

Example Link to heading

name = "Anna"
age = 25

# Basic formatting
print("Hello, %s! You are %d years old." % (name, age))

In this example, the format codes %s are used to format the name and %d to format the age.

Format codes: Link to heading

  • %s: Character string
  • %d: Integer
  • %f: Floating point number
  • %c: Character
  • %e: Scientific notation
  • %g: General notation

Example Link to heading

number = 123.456789

# Formatting with different formatting codes
print("Integer: %d" % number)
print("Floating point number: %f" % number)
print("Scientific notation: %e" % number)
print("General notation: %g" % number)

Exercise Link to heading

  • Write a Python code that prints a message returning a book to the library. the message should say “Hello, name_you_choose. You returned the book ‘book_you_choose’ on date_you_choose

You can guide yourself with the following template:

user_name =
book_title =
return_date =

message =

print(message)
<< Basic Operators Basic String Operations >>