Aditya Tiwari Profile picture
Feb 20, 2023 18 tweets 7 min read Read on X
F-strings in Python : The ultimate usage tips ⚡

F-strings in Python are a concise and powerful way to format strings. You can include any valid Python expression inside the curly braces in an f-string.

Thread 🧵

#Python #programming Image
To use an f-string, just prefix your string with the letter "f".

Here's an example that uses an f-string to format a variable:

#Python
1/16 Image
You can use f-strings to format numbers as well. Here's an example that formats a floating-point number with two decimal places:

2/16 #Python Image
You can also use f-strings to format dates and times. Here's an example that formats a datetime object:

3/16 Image
F-strings can also include conditional expressions. Here's an example that uses a conditional expression to format a string:

4/16 Image
F-strings also support string interpolation for objects that have a __str__ or __repr__ method. Here's an example that formats a custom object:

5/16 Image
You can use f-strings to format dictionary values as well. Here's an example that formats a dictionary:

6/16 Image
F-strings can also include expressions with function calls. Here's an example that uses the len() function:

7/16 Image
F-strings can also include arithmetic expressions. Here's an example that adds two numbers:

8/16 Image
F-strings can include expressions with ternary operators. Here's an example that uses a ternary operator to format a string based on a boolean value:

9/16 Image
F-strings can include expressions with file I/O. Here's an example that reads a file and formats the contents using an f-string:

10/16 Image
You can use f-strings to format values with custom formatting functions. Here's an example that formats a date using a custom formatting function:

11/16 Image
You can use f-strings to format complex objects like dictionaries with nested keys. Here's an example that formats a nested dictionary:

12/16 Image
You can use f-strings to format and align tabular data. Here's an example that formats tabular data using f-strings:

13/16 Image
Using f-strings to write data to a file with a filename variable:
This example uses an f-string to display the number of bytes that were written to the file, as well as the filename itself.

14/16 Image
With f-strings, you can format strings quickly and easily with a minimum of fuss.

Give them a try and see how they can make your code more concise and readable!

15/16
That's all for this thread. Hope you loved these quick code snippets on f-strings.

If you did, follow me for more snippets on python : @TheGeekyB0y

I post about #Python and #javascript .

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Aditya Tiwari

Aditya Tiwari Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @TheGeekyB0y

Mar 2, 2023
Inheritance in Python

It is a way of creating a new class by deriving it from an existing class.

With inheritance, the new class inherits all properties and behaviors of the existing class, allowing to reuse code and create classes that build on each other

A thread🧵
#Python
In Python, we can define a class that inherits from another class using the syntax class DerivedClass(BaseClass):

This means that the DerivedClass is inheriting from the BaseClass.

1/10
Here is an example of how to define a class that inherits from another class:

In this example, we have a Animal class with a speak method that prints "I am an animal". We also have a Dog class that inherits from Animal and adds a bark method that prints "Woof!".

2/10
Read 13 tweets
Feb 28, 2023
Objects and Classes in Python🐍

Objects are instances of a class, and classes are the blueprint for creating objects.

In this thread🧵, we'll explore the concepts of objects and classes in depth, along with some examples.

#Python Image
First, let's define a class in Python.

To create a class, we use the class keyword followed by the name of the class. Here's a simple example:

In this example, we've created a class called MyClass with nothing inside it. Let's add some attributes to the class.

1/10 Image
Attributes are variables that belong to a class. They are defined in the __init__ method of the class. Here's an example:

In this example, we've defined a class : Person with two attributes: name and age. The __init__ method is called when a new instance of the class is created. Image
Read 14 tweets
Feb 22, 2023
Strings and String Manipulation Methods in Python🐍

Strings in Python are a sequence of characters enclosed in single, double, or triple quotes.

Strings are immutable, which means you cannot change their values after they are created.

Detailed Thread👀🧵

#python #programming
Concatenation of Strings : You can concatenate two or more strings using the "+" operator.

1/16
Replication of Strings : You can also repeat a string multiple times using the "*" operator.

2/16
Read 18 tweets
Feb 21, 2023
Loops in Python ⚡🐍

Loops are an essential programming concept that allow you to execute a block of code repeatedly. There are two main types of loops in Python: for loops and while loops.

A thread 🧵#python #DataScience
For Loop :

A for loop allows you to iterate over a sequence of values, such as a list, tuple, or string.

Here's an example that prints each element of a lists

1/10
You can also use the range() function to iterate over a sequence of numbers. Here's an example that prints the numbers 0 through 9:

2/10
Read 17 tweets
Jan 27, 2023
Internet is full of resources. Its good, but sometimes, people get stuck on which is best for them.

I have compiled a massive list of resources for PYTHON lang you can get started with.

#Python

A thread 🧵
Official SitePython.org
Documentation : docs.python.org/3/
Quick Start Guide : python.org/about/gettings…

#Python

1/9
Basic Roadmap (GFG)
geeksforgeeks.org/best-way-to-st…

Python Notes (goalkicker)
books.goalkicker.com/PythonBook/

#Python

2/9
Read 11 tweets
Jan 7, 2023
Optimize your Python code using these 6 ways.

A thread 🧵
1. Use the 'timeit' module to measure the performance of your code.

This will give you a baseline to work from and help you identify areas of your code that are particularly slow.
2. Use the 'cProfile' module to profile your code and identify bottlenecks.

This will give you a detailed breakdown of where your code is spending most of its time, so you can focus your optimization efforts on the most impactful areas.
Read 8 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(