1# f-strings are short for formatted string like the following
2# you can use the formatted string by two diffrent ways
3# 1
4name = "John Smith"
5print(f"Hello, {name}") # output = Hello, John Smith
6
7# 2
8name = "John Smith"
9print("Hello, {}".format(name)) # output = Hello, John Smith
10
1>>> name = "Eric"
2>>> age = 74
3>>> f"Hello, {name}. You are {age}."
4'Hello, Eric. You are 74.'
5
1# This answer might be long, but it explains more python f-strings, how to use them and when to use them.
2# Python f-strings are used to write code faster.
3# Here is an example:
4name = "George"
5age = 16
6favorite_food = "pizza"
7
8# Instead of doing this:
9print("My name is", name, ", my age is", age, ", and my favorite food is", favorite_food)
10
11# Or this:
12print("My name is "+ name +", my age is "+ str(age)+ ", and my favorite food is "+ favorite_food)
13
14# You could do this:
15print(f"My name is {name}, my age is {age}, and my favorite food is {favorite_food}")
16
17# You see that the code looks a little cleaner, and as you start using f-strings you realize you write much faster.
18"""
19Why put the f before the string, you ask?
20Well if you didnt, the output would literally be {name} instead of the actual variable
21One more thing: this is fairly new and only works with python 3.6 and higher.
22"""
1"""
2
3An f-string stands for 'function-string' it's just used to work with
4strings more appropiately, they do the exact same job as concantenating
5strings but are more efficient and readable.
6
7"""
8# Concantenating strings:
9
10Age = "25"
11
12print("I am "+Age+" years old.")
13
14# Using f strings:
15
16Age = 25
17
18print(f"I am {Age} years old.")
19
20# ^ notice the letter 'f' at the begining of the string.
21# That defines the string as being an f-string.
22
23# A third way of inputting variables into a string is by using
24# .format()
25
26Age = "25"
27
28print("I am {} years old.".format(Age))
29
30# If you had more than one variable:
31
32Age = "25"
33Name = "Jeff"
34
35print("I am {} years old, and my name is {}.".format(Age,Name))
1#python3.6 is required
2age = 12
3name = "Simon"
4print(f"Hi! My name is {name} and I am {age} years old")