String Formatting in Python

Level 399 keystrokes

This shows two string formatting methods: f-strings and format(). Both format the score to one decimal place.

Python code

# The name to put in the output
1name = "Alice"
# A float score to format
2score = 95.5
# In an f-string, :.1f shows the value with 1 decimal place: "Alice: 95.5%"
3print(f"{name}: {score:.1f}%")
# str.format() fills the {} placeholders in order and prints the same line
4print("{}: {:.1f}%".format(name, score))

Output

Alice: 95.5%
Alice: 95.5%
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Python code and how often they appear.

  • "× 6
  • :× 4
  • .× 4
  • {× 4
  • }× 4
  • (× 3
  • )× 3
  • %× 2
  • =× 2
  • ,× 1

More Python examples at level 3

← Python typing practice