f-string vs .format() vs % in Python: which should I use?
Use f-strings. The other two exist for backward compatibility and for one narrow case.
2 min Read107 focused articles across 8 tracks. Each one opens with the answer, shows working code, and points at the deeper lesson if you want it.
Nothing matches that yet. Try a different track or search term.
Use f-strings. The other two exist for backward compatibility and for one narrow case.
2 min ReadJust use if not items. Here is why that is preferred over comparing the length.
Assignment does not copy. Here are the four ways that actually do — and when a shallow copy is not enough.
2 min Readenumerate() — and the start=1 argument almost everyone misses.
2 min ReadUse with open(...). Here is why, plus the encoding argument you should always pass.
2 min ReadThree ways, and the one difference between them that actually matters.
2 min Readsorted() with a key function, then rebuild the dict. Two lines.
2 min ReadOne line, no temporary variable. Here is what actually happens.
2 min Readstr.join() — and the one error it always throws first.
2 min ReadCatch what you can handle, name the exception, and never write a bare except.
2 min ReadA one-line way to build a list from another iterable — and the rule for when not to.
2 min ReadYes. It takes ten seconds and prevents the single most common Python setup problem.
2 min ReadOne asks "same value?", the other asks "same object?". Mixing them up produces bugs that pass your tests.
2 min ReadLists change, tuples do not — and that one difference drives everything else.
2 min Readmatch only looks at the start of the string. This is the number one regex mistake in Python.
2 min ReadBrackets crash on a missing key. get() returns None. Both are correct in different situations.
2 min Readinput() cannot know what you meant, so it always hands back text. Convert it the moment it arrives.
2 min ReadThe classic mutable default argument trap, and the one-line fix.
2 min ReadTwo are for output, two are for debugging. Using the wrong one hides the bug you are hunting.
2 min ReadGET to read, POST to change. Thirty years on, that rule still decides it.
2 min ReadSomething printed before your header() call. Usually a stray blank line after a closing ?> tag.
2 min ReadYou read a key that is not there. The null coalescing operator fixes it in one character.
2 min ReadUse DateTimeImmutable, set a timezone explicitly, and store UTC.
2 min ReadNever trust the filename, never trust the reported type, and never store uploads where PHP can execute them.
2 min Readpassword_hash and password_verify. Three lines, and nothing else.
2 min ReadPrepared statements. Not escaping, not filtering, not a blocklist of keywords.
2 min Readjson_encode and json_decode, plus the flags that stop them failing silently.
2 min ReadA cookie holds an ID; the data stays on the server. Plus the one call that prevents session fixation.
2 min ReadOne warns and continues, the other stops. Choose based on whether the file is essential.
2 min ReadDouble quotes expand variables and escapes. Single quotes do not. That is the whole rule.
2 min ReadTwelve functions that replace most of the loops you were about to write.
2 min ReadIt stops user input from becoming executable HTML. Use it on every echo of user data.
2 min ReadDependency manager and autoloader. It ends manual require statements.
2 min ReadNull coalescing — like the ternary, but it does not warn on missing keys.
2 min ReadLoose comparison converts types before comparing. Use === and the surprises stop.
2 min ReadA white screen is a fatal error with error display turned off. Two lines make it visible.
2 min ReadAll three are chunks of code you pass around. Two differences separate them.
2 min Readeach is for doing, map is for making. The single most important distinction in Ruby.
2 min ReadSafe navigation, ||=, and the difference between nil? and empty?
2 min ReadTwo dots include the end, three exclude it — plus the places ranges show up beyond loops.
2 min ReadDouble quotes and #{}. Plus format() for when you need columns.
2 min ReadOne searches the load path, the other resolves against the current file.
2 min ReadKeep matches, drop matches, or stop at the first one.
2 min ReadThe safe navigation operator — call a method only if the receiver is not nil.
2 min ReadIt writes your getter and setter methods for you.
2 min ReadGems are Ruby packages. Bundler makes sure everyone gets the same versions.
2 min ReadSymbols are immutable names. Use them as identifiers, strings as data.
2 min ReadAccess, iteration, defaults and the transform methods worth knowing.
2 min ReadList for almost everything. Arrays for fixed-size data and interop.
2 min ReadSomething is null that you assumed was not. Here is how to find it and how to prevent the next one.
2 min ReadFree the thread while waiting. Plus the two rules that prevent deadlocks.
2 min ReadCan-do versus is-a. And the practical rule: a class can implement many interfaces but inherit one base.
2 min ReadInterpolated strings, format specifiers, and why StringBuilder exists.
2 min ReadStructs copy, classes share. That difference explains most surprising C# behaviour.
2 min ReadQuery syntax for collections. Ten methods cover almost everything.
2 min Readif takes exactly three parts. cond handles any number of branches.
2 min Readdefine names things for a whole body; let names them for one expression.
2 min ReadA base case, and a step that makes the problem smaller. Always exactly those two.
2 min ReadA list is an item plus a smaller list. That recursive definition drives everything.
2 min ReadPrefix notation removes operator precedence entirely. That is the trade.
2 min ReadWhat each one is positioned against — and why absolute needs relative on the parent.
2 min ReadTransitions respond to a state change. Animations run on their own.
2 min ReadDebounce waits for a pause. Throttle enforces a maximum rate.
2 min ReadOne dimension or two. That is the whole decision.
2 min ReadDefine colours as custom properties, then redefine a handful of them. No component changes.
2 min ReadThree lines with flexbox, two with grid. Both directions, no hacks.
2 min Readfetch plus await, and the response.ok check that everyone forgets.
2 min ReadThe most common JavaScript error. Read it carefully — it names the exact property.
2 min ReadRight format, right size, lazy loading, and always set width and height.
2 min ReadKey-value strings that survive a refresh, plus the try/catch you actually need.
2 min ReadThe browser does most of it. Here are the attributes, and where they stop.
2 min ReadSyntax over promises that lets asynchronous code read top to bottom.
2 min ReadUse const by default, let when you must reassign, and var never.
2 min ReadTransform, select, collapse. The three that replace most of your loops.
2 min Readrem for almost everything, em for things that scale with their own text, px for hairlines.
2 min ReadUse === always. There is exactly one defensible exception.
2 min ReadTwo extra elements per selector, free, with no markup. Plus the property that makes or breaks them.
2 min ReadLet the content decide. But if you want numbers, here are four that work.
2 min ReadIt depends on how the function was called — except for arrow functions, which is why they exist.
2 min ReadIt makes width mean what you think it means. Put it in every stylesheet.
2 min ReadOne listener on a parent instead of one per child — and it works for elements added later.
2 min ReadSemantic elements give you structure, keyboard behaviour and screen-reader support for free.
2 min ReadUsually specificity. Here is how to read it, and what to do instead of !important.
2 min ReadEither the element is not positioned, or it is trapped in a stacking context.
2 min ReadMargin collapsing, inline whitespace, or line-height. Three different causes, three different fixes.
2 min ReadArrays are fast to read, slow to insert. Linked lists are the reverse — and lose more often than textbooks suggest.
2 min ReadMerge preserves history. Rebase rewrites it into a straight line. One rule decides.
2 min ReadOpen the file, pick what should be there, remove the markers, commit.
2 min ReadFour commands, depending on whether you want to keep the changes and whether it is pushed.
2 min ReadA function turns a key into an array index. That is the whole trick.
2 min ReadMatch the shape of the data. Recursive data wants recursive code.
2 min ReadLast in first out, versus first in first out. Which one you need tells you which algorithm you are writing.
2 min ReadA dozen commands cover most day-to-day work.
2 min ReadThe first digit tells you almost everything.
2 min ReadA lookup structure that turns a full table scan into a direct jump — at the cost of slower writes.
2 min ReadA contract for talking to someone else's program. Usually over HTTP, usually returning JSON.
2 min ReadHow the work grows as the input grows. Six classes cover almost everything you will meet.
2 min ReadSomething wrote UTF-8 and something else read it as Latin-1. Fix the declaration, not the text.
2 min ReadTests let you change code without fear. Start with the logic that would be embarrassing to get wrong.
2 min ReadPick one template and use it every time. Here is the one that works.
2 min ReadEveryone can solve it. The variations reveal how you actually think.
2 min ReadThree pointers, one pass. The classic pointer-manipulation question.
2 min ReadThe lookup table worth internalising before an interview.
2 min ReadThe problem that teaches the single most useful interview optimisation.
2 min ReadAnything with nesting is a stack problem. This is the cleanest example.
2 min ReadTurn a nested loop over subarrays into a single pass with two pointers.
2 min ReadSilent coding gives the interviewer nothing to assess. Here is the sequence to narrate.
2 min Read