f-string

f-string e Logical Operators OR?

f-string e Logical Operators OR? Question: I have a very simple problem that I never thought I’d encounter with the or operator and an f-string. The problem is that one of the phrase_1_random random variables is always printed. While phrase_2_random is NEVER printed. What am I doing wrong? I DON’T HAVE TO PRINT THEM BOTH …

Total answers: 3

Store formatted strings, pass in values later?

Store formatted strings, pass in values later? Question: I have a dictionary with a lot of strings. Is it possible to store a formatted string with placeholders and pass in a actual values later? I’m thinking of something like this: d = { "message": f"Hi There, {0}" } print(d["message"].format("Dave")) The above code obviously doesn’t work …

Total answers: 3

f-string formatting: display number sign?

f-string formatting: display number sign? Question: Basic question about python f-strings, but couldn’t find out the answer: how to force sign display of a float or integer number? i.e. what f-string makes 3 displayed as +3? Asked By: M. Page || Source Answers: use if statement if x > 0: .. "" else: . Answered …

Total answers: 5

How to use a variable as dictionary key using f-string in python?

How to use a variable as dictionary key using f-string in python? Question: I’m trying to print dictionary item value of a dictionary but my item is also an another user input variable. How can I print the selected value with f-string? option = ” languages = {‘1′:’Learn Python’, ‘2’:’Learn Java’, ‘3’:’Learn C++’, ‘4’:’Learn PHP’, …

Total answers: 4

What does the colon and the dot in a print statement mean?

What does the colon and the dot in a print statement mean? Question: What are . and : doing in a print statement? For example: print(f’The estimated revenue for a $350 film is around ${revenue_estimate:.10}.’) this returns : The estimated revenue for a $350 film is around $600000000.0. But without : and . the result …

Total answers: 2

f-string: unmatched '(' in line with function call

f-string: unmatched '(' in line with function call Question: I’m trying to use f-strings in python to substitute some variables into a string that I’m printing, and I’m getting a syntax error. Here’s my code: print(f"{index+1}. {value[-1].replace("[Gmail]/", ”)}") I only started having the problem after I added the replace. I’ve checked plenty of times and …

Total answers: 4

Truncate f-string float without rounding

Truncate f-string float without rounding Question: I want to print a very very close-to-one float, truncating it to 2 decimal places without rounding, preferably with the least amount of code possible. a = 0.99999999999 print(f'{a:0.2f}’) Expected: 0.99 Actual: 1.00 Asked By: Michael Bianconi || Source Answers: I don’t think you need f-strings or math functions, …

Total answers: 3

Insert newline after equals sign in self documenting f-string in python3.8

Insert newline after equals sign in self documenting f-string in python3.8 Question: With python3.8, a new feature is self documenting format strings. Where one would normally do this: >>> x = 10.583005244 >>> print(f”x={x}”) x=10.583005244 >>> One can now do this, with less repetition: >>> x = 10.583005244 >>> print(f”{x=}”) x=10.583005244 >>> This works very …

Total answers: 2

What does = (equal) do in f-strings inside the expression curly brackets?

What does = (equal) do in f-strings inside the expression curly brackets? Question: The usage of {} in Python f-strings is well known to execute pieces of code and give the result in string format (some tutorials here). However, what does the ‘=‘ at the end of the expression mean? log_file = open(“log_aug_19.txt”, “w”) console_error …

Total answers: 5

Why is f'{{{74}}}' the same as f'{{74}}' with f-Strings?

Why is f'{{{74}}}' the same as f'{{74}}' with f-Strings? Question: f-Strings are available from Python 3.6 and are very useful for formatting strings: >>> n=’you’ >>> f’hello {n}, how are you?’ ‘hello you, how are you?’ Reading more about them in Python 3’s f-Strings: An Improved String Formatting Syntax (Guide). I found an interesting pattern: …

Total answers: 1