-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay29.py
More file actions
65 lines (49 loc) · 1.89 KB
/
Day29.py
File metadata and controls
65 lines (49 loc) · 1.89 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
60
61
62
63
64
65
# f-strings in python
# string formatting can be done in python using format method.
# Example
print("Example 1")
txt = "For only {price:.2f} dollars!"
print(txt.format(price=49))
# f-strings in python
# It is a new string formatting mechanism introduced by the PEP 498.
# It is aka Literal string Interpolation or more commonly as f-string.
# The primary focus of this mechanism is to make the interpolation easier.
# When we prefix the string with the letter 'f', the string becomes the string itself.
print("\nExample 2: Old ways")
letter = "Hey my name is {} and I am from {}"
country = "India"
name = "Light"
print(letter.format(name, country))
print("\nExample 3")
print(f"Hey my name is {name} and I am from {country}")
# f-string is newly introduced 3.6 onwards
print("\nExample 4")
print(f"{2 * 30}")
print(type(f"{2 * 30}"))
print("\nExample 5")
# if we want to retain the raw string without replacing the variables values use double {{
print(f"We use f-strings like this!!,Hey my name is {{name}} and I am from {{country}} ")
# More Examples
print("\nExample 5")
language = "Python"
school = "freeCodeCamp"
print(f"I'm learning {language} from {school}.")
# How to Evaluate Expressions with Python f-Strings
print("\nExample to show how to Evaluate Expressions with Python f-Strings")
num1 = 83
num2 = 9
print(f"The product of {num1} and {num2} is {num1 * num2}.")
# How to Use Conditionals in Python f-Strings
print("\n How to Use Conditionals in Python f-Strings")
num = 87
print(num)
print(f"Is num even? {True if num % 2 == 0 else False}")
# How to Call Methods with Python f-Strings
print("\nHow to Call Methods with Python f-Strings")
author = "jane smith"
print(f"This is a book by {author}.")
# To print out the author's name formatted in title case
print("\nTo print out the author's name formatted in title case")
author = "jane smith"
a_name = author.title()
print(f"This is a book by {a_name}.")