xxxxxxxxxx
#python3.6 is required
age = 12
name = "Simon"
print(f"Hi! My name is {name} and I am {age} years old")
xxxxxxxxxx
# 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.
"""
xxxxxxxxxx
# 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
xxxxxxxxxx
>>> name = "Eric"
>>> age = 74
>>> f"Hello, {name}. You are {age}."
'Hello, Eric. You are 74.'
xxxxxxxxxx
# 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 """
xxxxxxxxxx
print(f"|{variable:25}|") #normal
print(f"|{variable:<25}|") # left align
print(f"|{variable:>25}|") # right align
print(f"|{variable:^25}|\n") #middle align
print(f"Using Numeric {variable = }")
print(f"With two decimal places: {variable:.2f}")
print(f"With four decimal places: {variable:.4f}")
print(f"With two decimal places and a comma: {variable:,.2f}")
### credit to http://cissandbox.bentley.edu/sandbox/wp-content/uploads/2022-02-10-Documentation-on-f-strings-Updated.pdf
xxxxxxxxxx
>>> name = "Eric"
>>> age = 74
>>> f"Hello, {name}. You are {age}."
'Hello, Eric. You are 74.'
xxxxxxxxxx
#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
xxxxxxxxxx
# 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!!
xxxxxxxxxx
# Ques.1
num = int(input("Enter your number: "))
for i in range(1, 11):
# print(str(num) + " × " + str(i) + " = " + str(i*num))
# you can't add variables
print(f"{num} × {i} = {num*i}")
# you can add variables