Mike Driscoll Profile picture
Dec 3 4 tweets 2 min read
#Python includes a `format()` function that is built-in. This function is very similar to the string's `format()` method, but is more low-level than that method

docs.python.org/3/library/func…

#python_builtins_by_driscollis

🧵🐍👇
Here are some examples of how you might use #Python's `format()` function: Image
The `format()` function uses the Format Specification Mini-Language. You can see this documented here:

docs.python.org/3/library/stri…

You can use the Format Specification Mini-Language to specify alignment, fill, width, precision and more.

Here are a couple of examples: Image
Thanks for taking the time to learn about #Python's `format()` function with me.

Follow me to learn more about Python!

• • •

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

Keep Current with Mike Driscoll

Mike Driscoll 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 @driscollis

Dec 5
Let's talk about #Python introspection!

Introspection is the examination of Python objects. It gives you a window into your code that lets you learn about Python using Python itself!

Find out how in this mini-thread!

🧵👇🐍
There are multiple ways to introspect #Python code. You will learn about the following tools you can use for introspection:

🐍 type()
🐍 dir()
🐍 help()
🐍 sys
You can use the `type()` function in #Python to learn about any object's type.

This can help you figure out what is going on in your code when you are debugging.

Here are some examples: Image
Read 8 tweets
Nov 29
Today I am going to show you how to create 7️⃣ different file types with #Python:

🐍 Text file
🐍 CSV
🐍 XML
🐍 JSON
🐍 Tar file
🐍 PDF
🐍 Excel

Join me for this fun 🧵👇
1️⃣ Creating a text file with #Python is a breeze! The recommended method is to use Python's `with` statement as it will automatically close the file for you

That means you can create a text file with TWO lines of code if you want to:
2️⃣ Creating a CSV file with #Python can be accomplished using the `csv` module

Here's a small example:
Read 16 tweets
Nov 28
Let's talk about creating dictionaries with #Python 🐍

A dictionary is a key / value pair. The key must be of an immutable type, such as an integer or a string. The value can be of any type

🧵👇
You can create a #Python dictionary in several different ways.

The following shows the most common method of dictionary creation followed by using a dictionary comprehension
You can also create a #Python dictionary by using the `dict()` function
Read 4 tweets
Nov 27
#Python has several Easter eggs built in! 🐍🥚🐣🐇

An Easter egg is a fun little surprise in a programming language, video game or movie.

Let's talk about some of Python's cool Easter eggs!

🧵🐍👇
🥚1️⃣ - The most famous #Python Easter egg is probably The Zen of Python, which you will see if you "import this"
🥚2️⃣ - What you might not know is that Zen of Python's source is really interesting too:

github.com/python/cpython…
Read 10 tweets
Nov 27
Another great tool in #Python's `collections` module is the `defaultdict`! 🐍🔥

`defaultdict` is a subclass of the built-in dict class where the first argument is a factory attribute.

🧵🐍👇
Let's start out by creating a regular dictionary so you can see how `defaultdict` might help clean up your code.

Here you create a dictionary based on words in a string using a conditional statement to add the various words.

You can also see the output in the 2nd screenshot
Now if you use a `defaultdict` with an `int` factory, you can get rid of the conditional statement entirely, which makes your code shorter and easier to read! 🐍🔥
Read 8 tweets
Nov 24
Another great tool in #Python's `collections` module is the `defaultdict`! 🐍🔥

`defaultdict` is a subclass of the built-in dict class where the first argument is a factory attribute.

🧵🐍👇
Let's start out by creating a regular dictionary so you can see how `defaultdict` might help clean up your code.

Here you create a dictionary based on words in a string using a conditional statement to add the various words.

You can also see the output in the 2nd screenshot ImageImage
Now if you use a `defaultdict` with an `int` factory, you can get rid of the conditional statement entirely, which makes your code shorter and easier to read! 🐍🔥 ImageImage
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 on Twitter!

:(