-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path22.f-string.py
More file actions
47 lines (31 loc) · 1.16 KB
/
Copy path22.f-string.py
File metadata and controls
47 lines (31 loc) · 1.16 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
"""
f-string is a new way to format strings in Python 3.6 and above versions of Python.
It is a more readable and concise way to format strings.
It is also faster than the older formatting methods.
**Syntax:**
f"string {variable}"
**Example:**
name = "John"
age = 25
print(f"My name is {name} and I am {age} years old.")
Output:
My name is John and I am 25 years old.
"""
"""
# Example 1 [ Using format() method ]
s = "My name is {}, I am a {}"
name = "Rajkumar"
gender = "Male"
print(s.format(name, gender))
# To solve the same problem using format() method it takes more lines of code.
# So, f-string is shorter and more readable than format() method.
"""
# To solve the same problem using format() method it takes more lines of code.
# So, f-string is shorter and more readable than format() method.
# Using f-string method doing example 1 problem in a single line of code.
name = "Rajkumar"
gender = "Male"
print(f"My name is {name}, I am a {gender}")
# We are going to use f-string in the future projects very often
# because it is more readable and concise than the older formatting
# f-string is also faster than the older formatting methods.