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
2 answers

output centered text using F-strings with dictionaries

I want to output values from my dict using f string but if I use that codeprint(f"{*excel_values['object']}"), I have error SyntaxError: can't use starred expression here. How can i fix this problem? UPD: I have to use f string because i want to…
Egor
  • 45
  • 1
  • 1
  • 5
0
votes
2 answers

Python string formatting and padding

I was wondering if there is a way in Python > 3.6, using f-strings, to achieve the following: I have a number 2451545.00000 which I would like to print as %.5f but at the same time I'd like to have it centre justified e.g. using f-string {' '}^{20}.…
Abedin
  • 23
  • 5
0
votes
0 answers

f-string throws a SyntaxError

I get invalid syntax error when I try to iterate over a list of names saved in names.txt file. Here's the syntax names = [] with open("names.txt") as file: for line in file: names.append(line.rstrip().capitalize()) for name in…
Getu
  • 1
  • 1
0
votes
1 answer

How can I use f string to call a list index?

I've read this issue, but it didn't work here. I've a dataframe and want to add new column with the month index considering the position they've in a list. I don't want to create an if condition because I've more languages in real life. That's why I…
polo
  • 185
  • 1
  • 3
  • 11
0
votes
1 answer

combination of eval + f-strings + match-case

How do I make the following work, for i in ['int', 'bool']: x = eval(f'match 1: \ case {i}(): print(1)') that is I want to run a match-case f-string inside an eval.
apostofes
  • 2,959
  • 5
  • 16
  • 31
0
votes
1 answer

f-string affected by quotation

alien_o = {"Colour" : "Green"} print(f"The colour is now {alien_o["Colour"]}.") What's wrong with these lines of code? The closing bracket and the curly bracket is affected by the quotes, and I don't understand why.
ale123
  • 15
  • 7
0
votes
1 answer

Print arg name with format strings for debug output

It is common to use format strings (or f-strings) to print variables for debugging. However one needs to repeat the argument names if they should be printed as well. [first_name, last_name, age] = ["Randall", "Munroe",…
Pyfisch
  • 1,752
  • 1
  • 17
  • 29
0
votes
3 answers

Equivalent implementation using f-strings and eval

x = type.__dict__ x is x gives, True but, x = type.__dict__ operator = 'is' eval(f'{x} {operator} {x}') gives, SyntaxError: invalid syntax even, operator = 'is' eval(f'{x!r} {operator} {x!r}') gives, SyntaxError: invalid syntax how to get True…
apostofes
  • 2,959
  • 5
  • 16
  • 31
0
votes
1 answer

adding string variables to HTML body in Python without breaking content-ID

I'm trying to make an automated task that (1) collects CSV data about wildfires from the internet, (2) reads its contents to see where wildfires in the CSV have occurred in the past 3 days, (3) visualizes them on a map, (4) and sends an email to a…
emil
  • 25
  • 9
0
votes
0 answers

Is there a way to reference the value of a variable in a comment (just like in {} with f-strings)?

Is there a way to reference (call) the value of a variable in a comment, just like in f-strings? Kind of like the following: S = 7 scenarios = [i+1 for i in range(S)] # Solve this problem for {S} scenarios I know that this is how it works in…
GT1992
  • 79
  • 6
0
votes
1 answer

Modification of XML data with namespaces using Python f-string and Xpath

My program is giving errors while I take input using namespaces and XPATH with f-string in for modification in XML File This works fine and it show all the attributes of the XML file for x in…
0
votes
1 answer

How to use for loop to retrieve product URL's from multiple webpages with BeautifulSoup?

I'm using BeautifulSoup to grab all of the product URL's from a website that has 58 pages. I'm using a for loop combined with f-strings because I noticed that each page has exactly the same link except for the page number. However, I noticed that…
0
votes
1 answer

printing variables in an f-string with conditions

can't seem to print the variables inside the f-string with conditions. I'm running this on flask hence the flash. Tried removing the braces and interchanging the quotes from single to double but still doesn't work. The output should be values of the…
0
votes
2 answers

Unable to display a multi-line equation annotation in plotly (Python)

I'm attempting to create an annotation that consists of a fit equation and optimal parameters. I've used a string label that was also used to generate a similar output using matplotlib. The label has mathematical symbols/notation. A minimum…
tdpu
  • 400
  • 3
  • 12
0
votes
0 answers

How to save a nested array in function to another.py script?

I would like to save an array (please see example bellow) to another file with using write-core function HERE_MY_ARRAY = [0.01,0.02,0.4] attribute - for example: reject_request, open_session etc. f = open("methods.py", "w") f.write('''\ def…
sk306
  • 29
  • 4