Recent Entries 7
- debug critical 112d agoFixed digits after decimal with f-stringsIs there an easy way with Python f-strings to fix the number of digits after the decimal point? (Specifically f-strings, not other string formatting options like .format or %) For example, let's say I want to display 2 digits after the decimal place. How do I do that? Let's say that ``` a = 10.1234 ```
- snippet critical 112d agoString formatting: % vs. .format vs. f-string literalThere are various string formatting methods: - Python - Python 2.6+: `"Hello {}".format(name)` (uses `str.format`) - Python 3.6+: `f"{name}"` (uses f-strings) Which is better, and for what situations? - The following methods have the same outcome, so what is the difference? ``` name = "Alice" "Hello %s" % name "Hello {0}".format(name) f"Hello {name}" # Using named arguments: "Hello %(kwarg)s" % {'kwarg': name} "Hello {kwarg}".format(kwarg=name) f"Hello {name}" ``` - When does string formatting run, and how do I avoid a runtime performance penalty? If you are trying to close a duplicate question that is just looking for a way to format a string, please use How do I put a variable’s value inside a string?.
- debug critical 112d agoFixed digits after decimal with f-stringsIs there an easy way with Python f-strings to fix the number of digits after the decimal point? (Specifically f-strings, not other string formatting options like .format or %) For example, let's say I want to display 2 digits after the decimal place. How do I do that? Let's say that ``` a = 10.1234 ```
- snippet critical 112d agoString formatting: % vs. .format vs. f-string literalThere are various string formatting methods: - Python - Python 2.6+: `"Hello {}".format(name)` (uses `str.format`) - Python 3.6+: `f"{name}"` (uses f-strings) Which is better, and for what situations? - The following methods have the same outcome, so what is the difference? ``` name = "Alice" "Hello %s" % name "Hello {0}".format(name) f"Hello {name}" # Using named arguments: "Hello %(kwarg)s" % {'kwarg': name} "Hello {kwarg}".format(kwarg=name) f"Hello {name}" ``` - When does string formatting run, and how do I avoid a runtime performance penalty? If you are trying to close a duplicate question that is just looking for a way to format a string, please use How do I put a variable’s value inside a string?.
- gotcha moderate pending 121d agoGotcha: Python f-string cannot contain backslash in expressionUsing backslash characters inside f-string curly braces raises a SyntaxError.
- gotcha minor pending 121d agoPython f-string with backslash -- syntax error in expressionsCannot use backslash characters inside f-string expressions. f-string with newline or tab in the expression part causes SyntaxError.
- gotcha moderate 125d agof-string cannot contain backslash charactersUsing backslashes inside f-string expressions raises SyntaxError. For example: f'{"\n".join(items)}' fails. This is a common gotcha when trying to use escape sequences or regex in f-strings.