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

actually i want to use a title method over a text in f-string?

my_favourites=['lamborghini',"sea facing resort", '100 b dollar','3D animator'] line_1='and by that much money i want to own a '.title()+my_favourites[1].title()+' & a '+my_favourites[0].upper() print(line_1) …
-1
votes
1 answer

Having trouble getting URL fstring to work

I am working on a program that scrapes recommended cafes from a review website and prints them based on the suburb inputted by the user. I am using an f-string on the URL to help the program fetch cafe lists from various suburbs depending on what is…
deadant88
  • 920
  • 9
  • 24
-1
votes
2 answers

Strings Formatting and Kwargs

The following code works correctly: def myfunc(**kwargs): if 'fruit' in kwargs: print('my fruit of choice is {}'.format(kwargs['fruit'])) else: print('No Fruit Here') myfunc(fruit = 'apple', veggie = 'lettuce) The following code returns an…
JWingert
  • 111
  • 1
  • 8
-1
votes
1 answer

Can I assign an input prompt to a variable?

This is my code: print(f" the int of your number is {int(float(input('type in a number: ')))}") Is there a way to assign a variable based on {int(float(input('type in a number: ')))} so that I can re-use it?
-1
votes
2 answers

Can't I use the if statement with math operations?

I am writing some code in order to test if a year is a leap year or not. So I wrote: year = input("please enter a year: ") if (year % 4) == 0: print(f"{year} is a leap year.") else: print(f"{year} is a nonleap year.") And the error…
-1
votes
2 answers

Why do we need to put apostrophes around an f string in python?

I understand what f strings are and how to use them, but I do not understand why we need to have apostrophes around the whole f string when we have already made the variables a string. first_name = 'Chris' last_name = 'Christie' sentence = f'That…
-1
votes
1 answer

Formatted string literals in Python 3.6+

Formatted string literals in Python 3.6+ I was reading into formatted string literals (a.k.a f-string) which is a feature added from the Python 3.6 update. I find it easier to read then the previous ways to doing similar projects, but I am having…
TazerFace
  • 170
  • 12
-2
votes
0 answers

f-string option and params option not working as expected

f-string or a URL is not working as well as params is not constructing the URL and the results are not as expected I am trying to pass two variables in query parameters. Option…
-2
votes
1 answer

How to zero pad an f-string?

Like this is how you can 0 pad a format string for i in range(10): zeropad = "Value is{:03}.".format(i) print(zeropad) and you get result as Value is 000 Value is 001 and so on... So, how can i do the same thing with f-strings?? I tried…
-2
votes
1 answer

How to ignore fragment of f-string in python assert

Is it possible to ignore one fragment of f-string in python assertion, eg. assert results.to_dict() == { "comment": f"Retrying in 3600 seconds (1/5)", } How to make that any of tries 1-5 was assert as true in this line? Maybe…
lukos06
  • 187
  • 1
  • 4
  • 20
-2
votes
3 answers

I have a syntax error but I dont know why, I think my code is good

I have my function defined: def somme(n1 , n2): result = (n1 + n2) return (f"The sum of" {n1} "+" {n2} "is equal to" {result}) That I call like this: somme(1,1) I get error on {n1}: return (f"The sum of" {n1} "+" {n2} "is equal to" {result}) …
Leao
  • 1
-2
votes
1 answer

inserting a variable into an fstring using .replace()

I have a code something similar to bellow. name = 'Dave' message = f' is a really great guy!' message = message.replace('', '{name}') print(message) the variables are a little more complicated than this, and a user (who may not be…
-2
votes
1 answer

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

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…
Justin Henson
  • 43
  • 1
  • 9
-2
votes
1 answer

How to use *args or **kwargs in f-strings?

For example, I need *missing_vars to be an f-string in this logging. Because for logging.critical() one msg argument is needed, so I can't do this: logging.critical(f'Required environment variables are missing: {*missing_vars}') I can do it without…
finegorko
  • 57
  • 8
-2
votes
1 answer

Python f-strings: how to escape curly brackets with numbers in it

If you have to escape curly brackets in f-strings you can double them, so {{hello}} results in {hello}. The problem is, that the algorithm just concatenates the string and afterwards interprets this part as every other part. So if there is any…