is there any difference between using string.format() or an fstring?
What's better to use? (%), (f"string") or (.format())?
Why use string.format instead of f strings?
String formatting, what's the best practice ?
Videos
Every time i see someone use format() instead of an fstring i get confused considering fstring has much more readability and is a lot more merciful on the programmer when there is more than 1 variable going into the string.
I was trying to make a, silly program in which you can do some random things. I used arguments (%)in the past, but it's a bit hard to write like the identification of the variable, like there is so much types (%s, %f, %2f, %d, %g, %e...) and I want to take the easiest to write or the easiest to debug.
here are some examples of using each one:
using (%):
name = "Imaginary_Morning"
age = 13
print("This profile is called %s and it has %d years old." % (name, age))
using (f"string"):
name = "Imaginary_Morning"
age = 13
print(f"This profile is called {name} and it has {age} years old.")
using (.format()):
name = "Imaginary_Morning"
age = 13
print("This profile is called {} and it has {} years old.".format(name, age))Now AFAIK there are some times when you need to use .format() to do something (off the top of my head, displaying a number to a certain number of digits), but as far as just basic things like displaying an email address, something like:
return "{}.{}@email.com".format(first, last)
why not just do f"{first}.{last}@email.com?
I only ask this because this example is in a Corey Schafer video I'm watching and was wondering if I'm missing something or if this is just one of those bad habits that programmers more knowledgeable than me just might have