Questions tagged [f-string]

Literal string interpolation in Python

An f-string is the abbreviated name for a "formatted string" syntax that was introduced in Python 3.6 and extended in Python 3.7. They enable literal string interpolation:

>>> value = 80
>>> f'The value is {value}.'
'The value is 80.'

See PEP 498 for a comprehensive description of the feature, and also PEP 536 for the final grammar.

621 questions
0
votes
0 answers

F-string error in Python - What does the equals sign mean?

New to python. I am getting: Invalid syntax line 1 I understand that the fstring error can show up on line one. These lines are pretty much taken from this page in the python docs. There is syntax I am unfamiliar with {err=}. I tried searching…
Eric Snyder
  • 1,816
  • 3
  • 22
  • 46
0
votes
2 answers

how to requests.get() a variable within a variable

I'm using Python 3.8 I have a variable api_url that represents an API URL I extracted from a database. The URL can change depending on how I query the database. api_url = https://system.company.com/API?%40asset={asset} I now want to make an API get…
Wes
  • 23
  • 4
0
votes
3 answers

Execute f-string in function

I have a string = 'long company name with technologies in it' and want to replace all tokens starting with search_string ='techno' with a new token replace_string = 'tech'. I wrote a function: def group_tokens(company_name, string_search,…
Judy
  • 35
  • 5
0
votes
0 answers

How to use f-strings to distribute tuple values versus .format

So I just learned about f-strings, and I like how efficient they can be, but I was wondering if there's a way I can use them to replicate something I do for some extremely simple code (mostly just math calculations for myself): Say I have a function…
Netforce23
  • 15
  • 4
0
votes
1 answer

Understanding an uncommon python syntax

This is a hackerrank practice problem https://www.hackerrank.com/challenges/python-string-formatting/problem . It is not actually a difficult problem, but one of the solutions posted in the discussion is confusing. I am adding their function for…
Jihjohn
  • 398
  • 4
  • 19
0
votes
4 answers

Can we use f-strings to create lists in for loop and then use it to append the data in the same iteration?

Requirement: I want to run a for loop which would create a new list as per it's range Once the list is created, I want the list to append the data running in the iteration If I've range of 5 then 4 lists should be created and should be used to…
Bhavya Lodaya
  • 140
  • 1
  • 10
0
votes
1 answer

What is the correct way to postpone the usage of a f-string in 2022?

I already saw multiple threads about postponed usage of f-strings, but they are all relatively old. So here are some examples I found: # lambda-function as constant POSTPONED_F_STRING = lambda x, y, z: f"Use variables: {x}, {y}, {z}" #…
JulMai
  • 15
  • 3
0
votes
2 answers

I can't install or run beautifulsoup

I was trying to install beautifulsoup using pip and then there was a message that asked me to update pip so i did then when i run the code with beautiful soup it gives me a syntax error here is the code:[https://i.stack.imgur.com/GxCSO.png] import…
0
votes
3 answers

Python/Django f-strings interpreting a string as input

Running out of ideas where to look. I am trying to set up a string that can be edited outside my code and stored by the user and have that string feed into an f-string inside my code. Example: Take the following value that is stored in database…
JoeF
  • 53
  • 7
0
votes
2 answers

How to print italic text in f-strings in Jupyter Notebook

I am trying to use f-strings to print some parts of text in italic format. Browsing the web for "python"+"print"+"italic"+"f-strings", I found these ANSI codes should do the work : '\x1B[3m' and '\x1B[0m' ; although they don't : print(f'\x1B[3m…
Andrew
  • 926
  • 2
  • 17
  • 24
0
votes
1 answer

f-strings to symbols in sympy

Thank millions for sharing and caring! I need to create and use symbols in dynamic way. In fact, symbols are made based on user input and used in code so I must use f'strings to make them. As shown below: L= ['xCl', 'xNa'] for j in range(len(L)): …
Z. K
  • 7
  • 4
0
votes
3 answers

Python f-string parsing NoneType from dictionary

d = {'surname':"Doe",'name':"Jane",'prefix':"Dr."} f"""{d['prefix'] or ''} {d['name'][0]+'. ' or ''}{d['surname']}""" works, however d = {'surname':"Doe",'name':None,'prefix':"Dr."} f"""{d['prefix'] or ''} {d['name'][0]+'. ' or…
maxwhere
  • 125
  • 1
  • 9
0
votes
1 answer

f-string script conversion to earlier version of Python

I have a question in regards to comparing files using python. For context, the problem I am having is that I have two firewalls with different configurations (over 14000 lines each on Notepad++) and I need to find the differences and annotate…
Sinetic
  • 3
  • 1
0
votes
1 answer

unable to add the variable value into a query using f string in python

I am having a JSON script and I want to add a variable value into it using f-string in python but I was unable to do it. name = "harsha" query = """mutation { createUsers(input:{ user_name: f"{name}" …
0
votes
2 answers

How to use variable in triple quote raw text in python?

I have HTML text and I want to use variables in that raw HTML text. The text looks like this: js_getResults ="""
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88