-
Notifications
You must be signed in to change notification settings - Fork 41
/
fstrings.py
53 lines (40 loc) · 1.41 KB
/
fstrings.py
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
# ------------------------------------------------------------------------------------
# Tutorial on f-strings
# Using f-strings is a simple and fast method for String formatting
# ------------------------------------------------------------------------------------
# # Syntax of f-strings:
# We start the string with the keyword f, followed by our string in double quotes and {} is used as a placeholder for the values involved in the formatting.
# The following example makes it clearer:
name = "John"
age = 17
print(f"I am {name} and I am {age} years old")
# Output- I am John and I am 17 years old
# Note that F(in capitals) also works
# Using multi-line f-strings:
name = 'John'
age = 32
occupation = 'Web developer'
msg = (
f'Name: {name}\n'
f'Age: {age}\n'
f'Occupation: {occupation}'
)
print(msg)
# You can also write a multi-lines f-string with double quotes like docstrings:
msg_two = f"""
Name: {name}
Age: {age}
Occupation: {occupation}
"""
print(msg_two)
# Output:
# Name: John
# Age: 32
# Occupation: Web developer
# ------------------------------------------------------------------------------------
# Challenge:
# Using f-strings print the following:
# Hi, I am <name>, my hobby is <hobby>, and I'm from <location>
# name, hobby and location will be inputs from the user
# Do the same for a multi-line output.
# ------------------------------------------------------------------------------------