# This answer might be long, but it explains more python f-strings, how to use them and when to use them.
# Python f-strings are used to write code faster.
# Here is an example:
name = "George"
age = 16
favorite_food = "pizza"
# Instead of doing this:
print("My name is", name, ", my age is", age, ", and my favorite food is", favorite_food)
# Or this:
print("My name is "+ name +", my age is "+ str(age)+ ", and my favorite food is "+ favorite_food)
# You could do this:
print(f"My name is {name}, my age is {age}, and my favorite food is {favorite_food}")
# You see that the code looks a little cleaner, and as you start using f-strings you realize you write much faster.
"""
Why put the f before the string, you ask?
Well if you didnt, the output would literally be {name} instead of the actual variable
One more thing: this is fairly new and only works with python 3.6 and higher.
"""
# f-strings are short for formatted string like the following
# you can use the formatted string by two diffrent ways
# 1
name = "John Smith"
print(f"Hello, {name}") # output = Hello, John Smith
# 2
name = "John Smith"
print("Hello, {}".format(name)) # output = Hello, John Smith
# f-string is a format for printing / returning data
# It helps you to create more consise and elegant code
########### Example program ##############
# User inputs their name (e.g. Michael Reeves)
name = input()
# Program welcomes the user
print(f"Welcome to grepper {name}")
################ Output ################
""" E.g. Welcome to grepper Michael Reeves """
# ------------------- string format, f-string ----------------------------
# {} is placeholder
num1 = 5
num2 = 3
print(f'{num1} times {num2} is {num1 / num2:.2f}') #2f means print to 2 decimal precision
#5 times 3 is 1.67
#explicit call format() method
number1 = 'One'
number2 = 'Two'
number3 = 'Three'
# default(implicit) order
default_order = "{}, {} and {}".format(number1,number2,number3)
print(default_order)
# One, Two and Three
# order using positional argument
positional_order = "{1}, {0} and {2}".format(number1,number2,number3)
print(positional_order)
# Two, One and Three
# order using keyword argument
keyword_order = "{i}, {j} and {k}".format(j=number1,k=number2,i=number3)
print(keyword_order)
# Three, One and Two
# f-strings help in string concatenation
name = 'Psych4_3.8.3'
age = 23
job = 'programmer'
#USING OLD METHOD
print("I am %s a %t of age %u", %(name, job, age))
# USING F-STRING
print(f"I am {name} a {job} of age {age}")
# here you can even see whcih value is inserted in which place....
# the f means that it is an f string. DONT FORGET IT!!
import random
name = input("What is your name? ") #Gets needed input
value = int(input(f"Give random value, {name}: ")) # The {name} means it puts the variable name there
multiplier = random.randint(3, 6)
print("Now multiplying your value...")
complete_value = multiplier * value
print(f"Your value is... {complete_value}") # Same here with complete_value
>>> import datetime
>>> name = 'Fred'
>>> age = 50
>>> anniversary = datetime.date(1991, 10, 12)
>>> f'My name is {name}, my age next year is {age+1}, my anniversary is {anniversary:%A, %B %d, %Y}.'
'My name is Fred, my age next year is 51, my anniversary is Saturday, October 12, 1991.'
>>> f'He said his name is {name!r}.'
"He said his name is 'Fred'."
'''
In python, rather than adding pieces of string together, you can use
f-strings to insert variables into strings. This makes your code much
more easier to read.
'''
# OLD - WITHOUT F-STRINGS
name = 'Bob'
age = 12
print('This is ' + name + ', he is ' + age + ' years old.')
# NEW - WITH F-STRINGS
print(f'This is {name}, he is {age} years old') # Note the f in front
#f string can be used insted of + in print statments
age = 33
#instead of:
print("I am " + str(age)) #output: I am 33
#do this
print(f"I am {age}") #output: I am 33