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
1 answer

Python: Trouble generating list output into f-string

I'm trying to find a way to take data in a csv file and run it through a loop to create output for each row. For example, this csv file has 4 columns titled name, color, age, gender. I wrote this code to try and accomplish that. csv_file =…
jswdesign
  • 3
  • 1
0
votes
7 answers

Consider a String a f-string in Python

Suppose I have x = 3 s = "f'12{x}4'" How to consider s as f-string to print 1234, like writing print(f'12{x}4') when I print s, it prints it as it as: f'12{x}4'
0
votes
2 answers

What's wrong with the syntax in Python in Google Colab in my google accunt?

For a week now I have noticed this in my account on different notebooks. the situation with the syntax. Python stopped seeing f-lines. And the print function began to display everything in brackets for some reason.
0
votes
2 answers

Is there a way to strip digits before the decimal place from a float using f-string?

I'm trying to solve the following challenge: Write a function that takes a float and two integers (before and after). The function should return a float consisting of before digits before the decimal place and after digits after. Thus, if we call…
05c4r2009
  • 11
  • 1
0
votes
2 answers

f-string formatting with only constant expressions?

I'm trying to learn Python from the book: "Fluent Python, 2nd Ed" by Luciano Ramalho. Example 2-8. Unpacking nested tuples to access the longitude metro_areas = [ ('Tokyo', 'JP', 36.933, (35.689722, 139.691667)), ('Delhi NCR', 'IN',…
0
votes
2 answers

Is it possible to format strings in C like Python's f-strings?

In Python it's possible to format strings conveniently using f-strings: num = 12 print(f"num is {num}") # prints "num is 12" Is it possible to do something like this in C? Or something that is similar to that? Currently, to add variables to the…
Eladtopaz
  • 1,036
  • 1
  • 6
  • 21
0
votes
1 answer

python append two zipped lists into docx file

john = [...] jake = [...] for a,b in zip(john, jake): document = Document() document.add_paragraph(f"{a}---------------{b} \n") document.save("docxpy.docx") I have two lists and want to append their values as…
eyup.tatar
  • 75
  • 8
0
votes
2 answers

Formatted string with calculated values in Python

In JavaScript I can use a template literal and include calculate values. For example: var a = 3; var b = 8; var text = `Adding ${a} + ${b} = ${a+b}`; // Adding 3 + 8 = 11 I know python has f'…' strings and str.format() with placeholder. Is there…
Manngo
  • 14,066
  • 10
  • 88
  • 110
0
votes
1 answer

How to conditionally change pandas DataFrame values into f-strings?

I have a pandas DataFrame whose values I want to conditionally change into strings without looping over every value. Example input: In [1]: df = pd.DataFrame(data = [[1,2], [4,5]], columns = ['a', 'b']) Out[2]: a b 0 1 2 1 4 5 This is my…
0
votes
1 answer

How to pass in string as f-string into another method?

I'm planning on passing in multiple f-string based templates into another general method that iterates over databases and tables. My code: @staticmethod def query1_template(): query = ''' SELECT '{db}' as db_name, '{tbl}' as…
Ricardo Francois
  • 752
  • 7
  • 24
0
votes
1 answer

function argument as an input for f-string to change variables

def cleaning(input): name = str(input) read_file = pd.read_csv('#f"{name}".csv') print(read_file) cleaning(InputKeyword) My function must take an input keyword, and this will change the "name" to "#input" word. I was trying with…
user16133525
0
votes
0 answers

Syntax Error: fstrings in DataBricks Python notebook

I keep getting the below syntax error when trying to use fstring in a DataBricks Python notebook. What am I missing? Cell: %python test = 'yay' f'This works, {test}!' Results: SyntaxError: invalid syntax Running this same code in a Jupyter…
ericOnline
  • 1,586
  • 1
  • 19
  • 54
0
votes
2 answers

What's the simplest way to add '0' for month and day which are integers?

I have inputs month and day which are both int type, I want to pass them to a function to construct a path, and if month or day are numbers below 10, we add a 0 in front of the number: def construct_path(year, month, day): if month >= 10 or day…
wawawa
  • 2,835
  • 6
  • 44
  • 105
0
votes
1 answer

Leading '1's instead of leading '0's in Python

Instead of 007, I wish to print 117, using string formatting. But instead, it adds 12 empty characters before the 7. Tried using a variable too, in place of the 1. No luck. Please…
0
votes
1 answer

why a conditional_expressions inside an f-string is rendered as a syntax error

I am trying to compile an SQL Update query from values in a PySimpleGUI fields. I am using concatenated f-strings to do this. If any field is blank, one must substitute 'DEFAULT' as the value. To do that I tried to use a conditional_expressions,…
MeirG
  • 333
  • 2
  • 14