F String Format
F String Format
What is an f-string?
An f-string, short for "formatted
string", is a way to create strings
(text) that include variables or
expressions in Python. It makes
combining text and values much
simpler and more readable.
name = "paresh"
age = 29
# Using an f-string
sentence = f"My name is {name} and I am
{age} years old."
print(sentence)
f"My name is {name} and I am
{age} years old.": The f before the
string tells Python to look inside the
curly braces {}.
{name}: This gets replaced by the
value of the variable name, which is
"Alice".
{age}: This gets replaced by the
value of the variable age, which is
30.
So, sentence becomes: "My name is
Alice and I am 30 years old."
Another Example
If you want to show a calculation
inside a string:
x = 5
y = 10
print(result)
Here, {x + y} directly performs the
addition inside the string and
includes the result, so the output will
be: "The sum of 5 and 10 is 15."
DOC STRING :
What is a Docstring?
A docstring (short for
"documentation string") is a special
kind of comment in Python that is
used to describe what a function,
method, class, or module does. It's
like a note that explains the purpose
and usage of your code.