0% found this document useful (0 votes)
12 views4 pages

Understanding F-Strings and Docstrings in Python

F-strings, introduced in Python 3.6, allow embedding expressions within string literals for improved readability and conciseness. They are created by prefixing a string with 'f' or 'F' and using curly braces for expressions. Docstrings, written with triple quotes, document the purpose of functions and can enhance code readability, while f-strings can be used within functions for dynamic content.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Understanding F-Strings and Docstrings in Python

F-strings, introduced in Python 3.6, allow embedding expressions within string literals for improved readability and conciseness. They are created by prefixing a string with 'f' or 'F' and using curly braces for expressions. Docstrings, written with triple quotes, document the purpose of functions and can enhance code readability, while f-strings can be used within functions for dynamic content.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

F String in Python

What are f-strings?


F-strings, short for formatted string literals, were introduced in
Python 3.6. They provide a way to embed expressions inside
string literals, using curly braces {}.

Why use f-strings?


Concise and Readable: f-strings are more readable and concise
compared to older string formatting methods.
Direct Embedding: You can directly embed expressions within
the string.
How to use f-strings
To create an f-string, prefix your string with the letter f or F.
Then, inside the string, you can include expressions in curly
braces {} that will be evaluated at runtime.

Example
Let's go through a simple example:

name = "paresh"
age = 27

# Using an f-string to format the


string
greeting = f"Hello, my name is {name}
and I am {age} years old."

print(greeting)
Formatting Numbers
You can format numbers within f-strings as well:

Python

price = 49.99

# Formatting the number to 2 decimal


places
formatted_price = f"The price is $
{price:.2f}"

print(formatted_price)

n this example, .2f tells Python to format the number with 2


decimal places.

Summary
f-strings start with an f or F before the string.
Curly braces {} are used to embed variables or expressions
inside the string.
They make the code more readable and concise.
By using f-strings, you can easily and clearly embed and format
data within strings in Python.

What is a Docstring?
A docstring is a string literal that appears right after the
definition of a function, method, class, or module. It is used to
document what the function, class, or module does. This helps
in understanding the purpose and usage of the code.

Why Use Docstrings?


Documentation: Provides an easy way to understand the code
and its functionality.
Readability: Makes the code more readable and maintainable.
Help Function: Enables the use of Python's built-in help()
function to display documentation.
How to Write a Docstring
A docstring is written using triple quotes """ or '''.

def add(a, b):


"""
Add two numbers and return the
result.

Parameters:
a (int): The first number.
b (int): The second number.

Returns:
int: The sum of a and b.
"""
print(a+b);
return a + b
add(11,12);
Why Use f-strings in Docstrings?
While you typically don't use f-strings inside docstrings directly,
you might use them in your functions to:

Dynamic Content: Create strings that include dynamic content,


such as variable values or expressions.
Readability: Make the code more readable and concise by
embedding variables directly in the strings.
Summary
Docstrings are used to document the purpose and usage of
code.
Triple quotes """ or ''' are used to write docstrings.
f-strings can be used within functions to create dynamic strings.
By using docstrings, you can make your code self-documenting
and easier to understand, while f-strings help you format and
embed expressions within strings in a clean and readable way.

You might also like