| name | python-flynt-f-string-converter |
| description | Use flynt to convert old Python string formatting to f-strings. Activate when: (1) Converting %-formatting to f-strings, (2) Converting .format() calls to f-strings, (3) Modernizing string concatenation to f-strings, (4) Improving code readability through f-string adoption, or (5) Batch-converting legacy Python codebases. Use when this capability is needed. |
Python Flynt F-String Converter
Overview
Flynt is an automated tool that transforms Python string formatting from older styles (%-formatting and .format()) into modern f-strings, available since Python 3.6.
Why f-strings?
- More readable and concise
- Less prone to errors
- Faster execution than older methods
- Easier to maintain
Key Capabilities
- Automatic conversion: Transforms both %-formatting and .format() to f-strings
- Directory recursion: Processes entire projects at once
- Safe transformations: Skips complex cases that might change behavior
- String concatenation: Can convert
+ operations to f-strings (Python 3.9+)
- Static joins: Can convert
.join() on static lists (Python 3.9+)
Quick Reference
Basic Commands
flynt file.py
flynt src/
flynt --dry-run src/
flynt --stdout file.py
flynt -s '"Hello, %s" % name'
flynt -v src/
flynt -q src/
Conversion Control
flynt --no-tp src/
flynt --no-tf src/
flynt --transform-concats src/
flynt --transform-joins src/
flynt --line-length 100 src/
flynt --no-multiline src/
flynt --aggressive src/
CI/CD Integration
flynt --fail-on-change src/
flynt --dry-run src/
Transformations
Printf-style (%-formatting)
"Hello, %s" % name
"Hello, %s %s" % (first, last)
"Value: %d, Price: %.2f" % (count, price)
"%(name)s is %(age)d years old" % {"name": name, "age": age}
f"Hello, {name}"
f"Hello, {first} {last}"
f"Value: {count}, Price: {price:.2f}"
f"{name} is {age} years old"
.format() Style
"Hello, {}".format(name)
"Hello, {0} {1}".format(first, last)
"Hello, {name}".format(name=name)
"{} + {} = {}".format(a, b, a + b)
"{:.2f}".format(value)
f"Hello, {name}"
f"Hello, {first} {last}"
f"Hello, {name}"
f"{a} + {b} = {a + b}"
f"{value:.2f}"
String Concatenation (--transform-concats)
"Hello, " + name + "!"
"Value: " + str(count)
first + " " + last
f"Hello, {name}!"
f"Value: {count}"
f"{first} {last}"
Static Joins (--transform-joins)
", ".join(["a", "b", "c"])
" ".join([first, middle, last])
"a, b, c"
f"{first} {middle} {last}"
Configuration
pyproject.toml
[tool.flynt]
line-length = 88
transform-concats = true
transform-joins = false
aggressive = false
no-multiline = false
Global Config (Unix)
Create ~/.config/flynt.toml:
line-length = 100
verbose = true
Global Config (Windows)
Create ~/.flynt.toml:
line-length = 100
verbose = true
Pre-commit Integration
repos:
- repo: https://github.com/ikamensh/flynt
rev: '1.0.0'
hooks:
- id: flynt
Skip Specific Lines
message = "Hello, %s" % name
message = "Hello, %s" % name
Common Workflows
Convert Entire Project
git add -A && git commit -m "Before flynt conversion"
flynt src/
git diff
pytest
Gradual Adoption
flynt --dry-run src/
flynt src/module1.py
pytest tests/test_module1.py
flynt src/module2.py
pytest tests/test_module2.py
CI Pipeline Check
flynt --fail-on-change --quiet src/
Preview Mode
flynt --dry-run src/ 2>&1 | less
flynt --dry-run src/ > flynt-changes.txt 2>&1
Best Practices
- Use version control: Always commit before running flynt
- Run tests after: F-strings can expose subtle bugs in edge cases
- Review aggressive mode:
--aggressive may change behavior in edge cases
- Start conservative: Run without
--aggressive first
- Combine with other tools: Run Black/Ruff format after flynt
Caveats and Edge Cases
Behavior Differences
F-string conversion may alter behavior in edge cases:
'%s' % (1,)
f'{(1,)}'
Complex Expressions
Flynt skips conversions that would make code less readable:
"result: {}".format(
very_long_function_call_with_many_arguments(
arg1, arg2, arg3
)
)
Type Requirements
F-strings are stricter about types:
"count: %s" % some_object
f"count: {some_object}"
Comparison with Alternatives
| Tool | F-string Conversion | Other Features |
|---|
| Flynt | Specialized, thorough | Concat/join conversion |
| Ruff UP032 | Basic conversion | Full linter suite |
| Pyupgrade | Conservative conversion | Full syntax upgrades |
When to Use Flynt
- Primary focus on f-string conversion
- Need concat/join transformations
- Want specialized f-string tool
When to Use Ruff/Pyupgrade
- Already using these tools
- Want all-in-one solution
- Simpler conversions are sufficient
Detailed Reference
For comprehensive pattern examples, see references/fstring-patterns.md.
External Links
Converted and distributed by TomeVault — claim your Tome and manage your conversions.