-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf_string.py
More file actions
59 lines (46 loc) · 1.59 KB
/
Copy pathf_string.py
File metadata and controls
59 lines (46 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# f string is also used to format the string in python and
# it is more efficient than + operator to concatenate the string and variable.
name = "Alice"
age = 25
height = 1.75
# 1. Basic
print(f"Name: {name}, Age: {age}")
# 2. Expressions inside {}
print(f"Next year: {age + 1}")
print(f"Name in uppercase: {name.upper()}")
# another way
print("another way to format the string is by using format() method")
print("name is {} and age is {}".format(name, age))
# 3. Formatting numbers
print(f"Height: {height:.2f}") # 1.75
print(f"Height: {height:.1f}") # 1.8
print(f"Percentage: {0.75:.1%}") # 75.0%
# 4. Padding and alignment
print(f"{name:>10}") # Right align ( Alice)
print(f"{name:<10}") # Left align (Alice )
print(f"{name:^10}") # Center align ( Alice )
# 5. Dictionary access
person = {"name": "Bob", "age": 30}
print(f"{person['name']} is {person['age']}")
# 6. Function calls
print(f"Length of name: {len(name)}")
# 7. Multi-line f-strings
message = f"""
Name: {name}
Age: {age}
Height: {height:.2f}m
"""
print(message)
# 8. Debugging (Python 3.8+)
x = 10
print(f"{x = }") # Prints: x = 10
# any operation can be done inside the f string like addition, subtraction, multiplication, division, etc.
# Anything inside {} is evaluated/executed
print(f"{2 + 2}") # 4
print(f"{5 * 3}") # 15
print(f"{10 / 2}") # 5.0
print(f"{2 ** 10}") # 1024
text = "hello world"
print(f"{text.replace('world', 'Python')}") # hello Python
print(f"{text.split()}") # ['hello', 'world']
print(f"{text.upper().lower()}") # hello world