0% found this document useful (0 votes)
13 views

F String Format

Uploaded by

royalcommunity43
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

F String Format

Uploaded by

royalcommunity43
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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.

Why use f-strings?


Imagine you have some pieces of
information, like a person's name
and their age, and you want to
create a sentence that includes
these pieces of information. F-strings
make this task easy and clear.

How do f-strings work?


Start with the letter 'f': Begin your
string with the letter f or F.
Use curly braces {}: Put the
variables or expressions you want to
include inside curly braces {}.

Let's say you have a variable name


that stores a person's name and a
variable age that stores their age.
Here's how you can use an f-string to
create a sentence with these
variables:

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."

Why is this useful?


Easy to read and write: It's clear and
straightforward to see where the
variables are going in the string.
Reduces mistakes: You don't have to
worry about manually converting
variables to strings or concatenating
multiple parts together.
Flexible: You can include any
expression inside the curly braces,
not just variables.

Another Example
If you want to show a calculation
inside a string:

x = 5
y = 10

# Using an f-string to show the result


of x + y
result = f"The sum of {x} and {y} is {x
+ y}."

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.

Why use Docstrings?


Docstrings help:
Understand code: They make it
easier for others (or you, in the
future) to understand what your
code does.
Maintain code: They provide
information about how to use the
code, making maintenance easier.
Generate documentation: Tools can
use docstrings to create formal
documentation automatically.
How do you write a Docstring?
Triple quotes: Start and end your
docstring with triple quotes (""" or
''').
First line: Provide a brief summary of
what the function or class does.
Optional details: Add more detailed
explanation, including descriptions of
parameters and return values.
Example
Let's say you have a function that
adds two numbers. Here's how you
add a docstring to it:

You might also like