Skip to content

Commit a1f9593

Browse files
committed
Added f_string.py to Python-Basic-to-Advance repo
1 parent 9c699d4 commit a1f9593

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

f_string.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Define variables
2+
name = "Jenny" # Person's name
3+
age = 30 # Person's age
4+
height = 1.6 # Person's height in meters
5+
6+
# Using string concatenation (need to convert numbers to string with str())
7+
print("My name is " + name + "." + " I am " + str(age) + " Years old." + " My height is " + str(height) + " meter.")
8+
9+
# Using f-string with commas (adds spaces automatically between items)
10+
print(f"My name is ", name, ".", "I am ", age, " Years old.", " My height is ", height, " meter.")
11+
12+
# Proper f-string interpolation (clean and recommended way)
13+
print(f"My name is {name}. I am {age} Years old. My height is {height} meter.")
14+
15+
# Expression inside f-string (father's age is double the person's age)
16+
print(f"My father age is {age*2}")

0 commit comments

Comments
 (0)