Python
format string
string formatting
programming
Python tips

What does s mean in a Python format string?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Python formatting, s usually means string representation. You can see it in old percent formatting and in the format mini-language with str.format or f-strings. Understanding where s applies helps avoid type surprises and formatting bugs.

s in Percent Formatting

Old style formatting uses %s to insert a value as string.

python
name = "Ada"
msg = "Hello, %s" % name
print(msg)

%s calls string conversion, so non-string values still work.

python
value = 42
print("Value: %s" % value)

This style is still seen in legacy code.

s in str.format and f-strings

In format specifiers, s is a type code for strings.

python
name = "Linus"
print("{:s}".format(name))
print(f"{name:s}")

For strings, explicit s is optional in most cases.

python
print("{}".format(name))
print(f"{name}")

The explicit type code is useful when you combine alignment and width rules.

Width and Alignment with s

String formatting often uses alignment with optional width.

python
1name = "Grace"
2print(f"|{name:>10s}|")
3print(f"|{name:<10s}|")
4print(f"|{name:^10s}|")

These examples right-align, left-align, and center the text.

Difference Between s and r

In f-strings, !s and !r control conversion before formatting.

python
1class Item:
2    def __str__(self):
3        return "Item as str"
4
5    def __repr__(self):
6        return "Item as repr"
7
8x = Item()
9print(f"{x!s}")
10print(f"{x!r}")

Use !r for debugging details and !s for user-facing output.

Applying s format code to non-string in strict contexts can fail.

python
n = 12
# print(f"{n:s}")  # would raise ValueError
print(f"{str(n):s}")

If value is not string, convert first or use suitable numeric format code.

Choosing Formatting Style

Modern Python usually favors f-strings for readability and speed. %s remains common in older code and some logging contexts.

python
user = "Ada"
score = 97
print(f"User {user} scored {score}")

Keep style consistent within a codebase to reduce confusion.

Logging Note

For performance-sensitive logging, prefer deferred interpolation from logging library instead of immediate f-string evaluation.

python
1import logging
2
3logger = logging.getLogger(__name__)
4user = "Ada"
5logger.info("User %s logged in", user)

This avoids unnecessary formatting when log level is disabled.

Practical Formatting Guidelines

Use explicit format specifiers when output contracts matter, such as fixed-width reports or machine-parsed logs. Even though s is often optional, explicit formatting can make review intent clearer. For developer-facing logs, !r often provides better detail, while user-facing text should favor plain or !s conversion.

Balancing readability and explicitness is usually more important than strict loyalty to one formatting style.

For internationalized applications, keep translation templates separate from low-level format code so localization teams can adjust user-facing text without touching engineering-specific formatting internals.

In code reviews, check whether formatting expressions match their audience. Developer diagnostics and user-visible messages often need different conversion strategies for clarity and supportability.

When migrating legacy code, update formatting incrementally and keep behavior tests around edge cases such as null-like values and custom object display methods.

Team style guides should include formatting examples for strings, numbers, and debug output so contributors can choose the right conversion quickly without introducing inconsistent message formats.

Common Pitfalls

  • Mixing percent formatting and f-string syntax in the same expression.
  • Assuming :s works for any type without conversion.
  • Forgetting that explicit s is optional for most string insertions.
  • Using !s when debug output needs !r representation.
  • Applying style inconsistently across a codebase.

Summary

  • s indicates string formatting in Python formatting systems.
  • %s is old style string placeholder in percent formatting.
  • In f-strings and format, s is a string type specifier.
  • Convert non-string values when strict string specifiers are used.
  • Prefer consistent formatting conventions across your project.

Course illustration
Course illustration

All Rights Reserved.