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

Feb 22
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 Image
Concatenation of Strings : You can concatenate two or more strings using the "+" operator.

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

2/16 Image
Read 18 tweets
Feb 21
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 Image
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 Image
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 Image
Read 17 tweets
Jan 27
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
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
Dec 23, 2021
Twitter is free university.
But 98.8% don’t follow the right people.

Here are top creators you should follow :

1. Web development
2. Python
3. Web3 / Blockchain
4. Machine Learning/Data Science

#infosec
Read 7 tweets
Dec 23, 2021
Learning Python ?🐍

Here are my favourite YouTube creators/channels for learning Python.

#MachineLearning #100DaysOfCode
#Python #AI

A thread 🐍📝👇
1. Telusko

Here is complete playlist of Telusko's python includes 67 vids on python and others on Django as well.

youtube.com/playlist?list=…
2. Programming with Mosh @moshhamedani

His this youtube video is highly recommended for Python Beginners.
Read 7 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 on Twitter!

:(