f-Strings
'''
f-string(Formatted string literals) in Python is a way to embed expressions directly in string literals, making string formatting more readable and efficient.
Introduced in Python 3.6, f-strings are prefixed with the letter f or F and allow us to include expressions within curly braces {} that are evaluated at runtime.
'''
#Example:
message = "Hey My name is {} and I am from {}"
name = "Sindhu"
country = "India"
print(message.format(name, country))
#output = Hey My name is Sindhu and I am from India
print(message.format(country, name))
#output = Hey My name is India and I am from Sindhu
message1 = "Hey My name is {1} and I am from {0}"
print(message1.format(country, name))
#output = Hey My name is Sindhu and I am from India
print(f"Hey My name is {name} and I am from {country}")
#output = Hey My name is Sindhu and I am from India
print(f"Hey My name is {{name}} and I am from {{country}}")
#output = Hey My name is {name} and I am from {country}
txt = "For only {price: .3f} dollars."
print(txt.format(price = 1234.56789))
#output = For only 1234.568 dollars
print(f"{24 * 9}") #output = 216
print(type(f"{24 * 9}")) #output = <class 'str'>
Comments
Post a Comment