How to avoid creating a newline when using if-else in f-string expression

Advertisements Please see the below minimal example, printbbb = True print(f"""AAA {”’BBB”’ if printbbb else ”} CCC""") This prints AAA BBB CCC as desired, however, if I set printbbb to False, it prints AAA CCC How can I change the code so that it will print AAA CCC when printbbb is set to False? >Solution… Read More How to avoid creating a newline when using if-else in f-string expression

How to format f-strings from dataframe with curly brackets in the dataframe?

Advertisements How to format f-strings from dataframe with curly brackets in the dataframe? Text in a dataframe that has curly brackets { and } in the text, which is intended to trigger f-string formatting of defined variables in the Python code. But the code instead just yields the actual text like "{From}" instead of the… Read More How to format f-strings from dataframe with curly brackets in the dataframe?

Looping through directories in Python using f-string

Advertisements I am trying to access 2 different directories in ADLS using for loop & f-string but facing issues in doing so # Primary storage info account_name = "accountnamehere" # fill in your primary account name container_name = "logscontainer" # fill in your container name subscription_id = "subscriptionname" resource_group = "My-Resource-ML" # fill in your… Read More Looping through directories in Python using f-string

inserting a variable into an fstring using .replace()

Advertisements I have a code something similar to bellow. name = ‘Dave’ message = f'<name> is a really great guy!’ message = message.replace(‘<name>’, ‘{name}’) print(message) the variables are a little more complicated than this, and a user (who may not be programming literate) will have entered into a variable via input(). I’m wanting to convert… Read More inserting a variable into an fstring using .replace()

Python 3.9.12: f-string error – SyntaxError: invalid syntax

Advertisements I am using Spyder with Python 3.9.12 Here is the code I have inside Spyder: user_input = (input(‘Please enter a number between 1 and 12:>>’ )) while (not user_input.isdigit()) or (int(user_input) < 1 or int(user_input) > 12): print(‘Must be an integer between 1 and 12’) user_input = input(‘Please make a selection:>> ‘) user_input =… Read More Python 3.9.12: f-string error – SyntaxError: invalid syntax

Is it possible to use whitespace in format specifier

Advertisements If I have following print statements: print("#"*80) print(f"##{”:.^76}##") print(f"##{‘Hello World’:.^76}##") print(f"##{”:.^76}##") print("#"*80) I will get a nice border around my "Hello World" but with dots: ################################################################################ ##………………………………………………………………….## ##…………………………..Hello World……………………………## ##………………………………………………………………….## ################################################################################ If I use a ‘ ‘ instead of the . in the second to fourth print statement, I will get a ValueError: Invalid… Read More Is it possible to use whitespace in format specifier

Why are f-strings slower than string concatenation when repeatedly adding to a string inside a loop?

Advertisements I was benchmarking some code for a project with timeit (using a free replit, so 1024MB of memory): code = ‘{"type":"body","layers":[‘ for x, row in enumerate(pixels): for y, pixel in enumerate(row): if pixel != (0, 0, 0, 0): code += f”'{{"offsetX":{-start + x * gap},"offsetY":{start – y * gap},"rot":45,"size":{size},"sides":4,"outerSides":0,"outerSize":0,"team":"{‘#%02x%02x%02x’ % (pixel[:3])}","hideBorder":1}},”’ code += ‘],"sides":1,"name":"Image"}}… Read More Why are f-strings slower than string concatenation when repeatedly adding to a string inside a loop?