DEV Community

Cover image for Python3 Programming - Exercise 8 - String formatting
Michael Otu
Michael Otu

Posted on • Edited on

2 1

Python3 Programming - Exercise 8 - String formatting

String formatting

There are a couple of ways to format our output, making our output more readable. We would go with the f"... {some_value}" approach known as string interpolation (f-strings).
The {} provides the means to insert values into our string as we write it out. some_value is the value we pass into the string, whereby f means format. The string must begin with the f followed by string delimiters (double or single quote).

Example

# Consider a simple program to take the users
# name and age and then print them to the screen

name = input("Enter name: ")
age = input("Enter age: ")

print(f"User name: {name}\nUser age: {age}")

# Another sample code
# A program to find the area of a triangle
# The area of a triangle is half the product of the base
# (length) and height of the triangle

height = float(input("Enter height of triangle: "))
base = float(input("Enter base of triangle: "))
area = 0.5 * base * height

print(f"The area of a triangle of height, {height}")
print(f" and base, {base} has an area of {area}")
Enter fullscreen mode Exit fullscreen mode

When we have a float, such as, 3.14159 we could round it with the round(number, digit) function to 2-decimal places then print it out.

pi = 3.14159
rounded_pi = round(pi, 2)
print(f"pi, {pi}, rounded to 2d.p is {rounded_pi}")
Enter fullscreen mode Exit fullscreen mode

There is a simple way to do that using f-strings.

pi = 3.14159
print(f"pi, {pi}, rounded to 2d.p is {pi:.2f}")
Enter fullscreen mode Exit fullscreen mode

Practicals

Using the new concept in this exercise and the white space character, newline - \n and one print function. Write a program that takes users full name and their favourite number. Print to the screen the string, <full name> thinks <num / 3> is special.

Given <full name> as, Daniel and <number> as 23, print, Daniel thinks 7.667 is special. Divide the <number> by 3 and round it to 3 decimal places.

Summary

  • One may use f"... {}" to format a string
  • f - means format
  • {} allows us to insert values directly into the string

Heroku

Build AI apps faster with Heroku.

Heroku makes it easy to build with AI, without the complexity of managing your own AI services. Access leading AI models and build faster with Managed Inference and Agents, and extend your AI with MCP.

Get Started

Top comments (0)

Tiger Data image

🐯 🚀 Timescale is now TigerData: Building the Modern PostgreSQL for the Analytical and Agentic Era

We’ve quietly evolved from a time-series database into the modern PostgreSQL for today’s and tomorrow’s computing, built for performance, scale, and the agentic future.

So we’re changing our name: from Timescale to TigerData. Not to change who we are, but to reflect who we’ve become. TigerData is bold, fast, and built to power the next era of software.

Read more

👋 Kindness is contagious

Delve into a trove of insights in this thoughtful post, celebrated by the welcoming DEV Community. Programmers of every stripe are invited to share their viewpoints and enrich our collective expertise.

A simple “thank you” can brighten someone’s day—drop yours in the comments below!

On DEV, exchanging knowledge lightens our path and forges deeper connections. Found this valuable? A quick note of gratitude to the author can make all the difference.

Get Started