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

Insert multiple values into a string pandas

Suppose we have a string and a dataframe like this: import pandas as pd myStr="Insert {var1} here & here {var1} and insert {var2} here & here {var2}." df = pd.DataFrame({'Var1': (1, 2), 'Var2': ('B', 'C')} >>> df Var1 Var2 0 1 B 1 2 …
Alex Man
  • 457
  • 4
  • 19
0
votes
1 answer

Timeit: pass stmt argument with f-string format

I am trying to estimate the time for running an algorithm by timeit module. But I ran into a problem when passing the stmt argument with f-string. To demonstrate my problems, I create two simple functions to return Fibonacci number as an…
PWbio
  • 41
  • 2
  • 5
0
votes
0 answers

Any way to incorporate an f-string in an identifier?

Lets say I want to produce a number of variables, but the actual amount I am unaware of (as may depend on the input, etc.), the only thing differentiating the variables from one another is a piece of information in their identifier. Is there any way…
0isab
  • 67
  • 2
  • 7
0
votes
0 answers

Invalid Syntax in F string Python 3

When i have this in python3 it gives me a invalid syntax error, How would i fix this? for filename in os.listdir(f'{KFD}/{name}'): image = face_recognition.load_image_file(f"{KFD}/{name}/{filename}") encoding =…
0
votes
1 answer

How can I convert fstring to str.format?

HEADER_LENGTH = 10 my_username = input("Username: ") username = my_username.encode() username_header = f"{len(username):<{HEADER_LENGTH}}" How can I 'username_header' be changed from fstring to str.format()? I must run on python3.5. so I can't use…
Zerowest
  • 3
  • 1
0
votes
0 answers

Fixed digits of all list values after decimal with f-strings or pretty print list with f string

I have learnt numbers' output can be controlled well with f-string, is there any such magic with lists? I have a list of floats and want to print just 2 decimal point of each number. an example of what I want although it is not a pretty solution…
borgr
  • 20,175
  • 6
  • 25
  • 35
0
votes
2 answers

F strings giving syntax error in python 3.7.6

This code: a = 10 b = 20 print(f "The variable a is {a} and the variable b is {b}.") returns this error: File "main.py", line 3 print (f "The variable a…
Vijay Shastri
  • 53
  • 1
  • 5
0
votes
1 answer

when converting XML to SEVERAL dataframes, how to name these dfs in a dynamic way?

my code is on the bottom "parse_xml" function can transfer a xml file to a df, for example, "df=parse_XML("example.xml", lst_level2_tags)" works but as I want to save to several dfs so I want to have names like df_ first_level_tag, etc when I run…
neutralname
  • 383
  • 2
  • 4
  • 11
0
votes
1 answer

Why does indexing anonymous tuple in f-string give an error?

I am trying to understand why this is happening: >>> selection = False >>> a = ("NO", "YES") >>> print(f"{a[int(selection)]}") NO >>> print(f"{("NO", "YES")[int(selection)]}") File "", line 1 print(f"{("NO", "YES")[int(selection)]}") …
Tammi
  • 421
  • 7
  • 12
0
votes
1 answer

Using version Python 3.7.4 and still getting syntax error

I'm getting a syntax error on my f-string statements even though i'm using python version 3.7.4 my_name= "raushan" print(f"Let's talk about {my_name}") File "", line 2 print(f"Let's talk about {my_name}") …
Ganesh
  • 19
  • 5
0
votes
1 answer

Why can't I call dict.get() in f-string? (Python 3.8.1)

Question Does anybody have a beginner-friendly explanation as to why I get a SyntaxError when I call .get() from within an f-string in Python >3.8? Problem When I call dict.get() directly within the f-string I get a SyntaxError. If I store the…
p6l-richard
  • 321
  • 2
  • 11
0
votes
1 answer

SyntaxError: invalid syntax in a python function

I don't see the issue in the function, I hope you can shed some light to the issue below. Thanks in advance. Python function: def gen_histograms(dataframe, cols=1, file=None): rows = math.ceil(len(dataframe.columns)/cols) figwidth =…
Cassie.L
  • 311
  • 1
  • 7
  • 19
0
votes
0 answers

Simple f-string claiming to be invalid syntax when it was copied and pasted

I am on day two of starting to learn Python and already got stuck on what will be a really simple problem. I am trying to do an f-string to type out my name but for some reason, it's saying my syntax is invalid. I typed it in exactly how it is in…
0
votes
2 answers

What does the f do in plt.ylabel(f'g(x)')?

Firstly, import matplotlib.pyplot as plt import matplotlib In an elementary example on how to plot with matplotlib in our lecture I encountered the following line plt.ylabel(f'g(x)') What I have tried In the documentation there's no mention of an…
ViktorStein
  • 103
  • 8
0
votes
0 answers

Python3 using f-string with regex in findall

I tried to use re.findall to look for some string to parse to information I want to pattern from a large html file. the information I need is 0000-01-02-03-04-asdasdad that sit in the string like…
jacobcan118
  • 7,797
  • 12
  • 50
  • 95