Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. โ Today: We learn "f-strings" - write variables and expressions directly inside a string โ
f-strings are fast, neat way to put values into a string in Python. "Place an f" and use "curly braces {}".
โ Example
In:
name = "Sam" marks = 88 result = f"{name} scored {marks} marks" print(result)
Out: Sam scored 88 marks
โ How it works:
โ More examples and features... โ ๐งฎ Math on the go
In:
a, b = 5, 3 print(f"Sum = {a + b}")
Out: Sum = 8
โ ๐ Control number of decimals you want to see
In:
pi = 3.14159 print(f"{pi:.2f}") # for 2 decimals
Out: 3.14
โ ๐ฐ If your boss likes numbers separated by commas
In:
num = 1000000 print(f"{num:,}")
Out: 1,000,000
โ ๐ค Align your output - left, right & center
In:
word = "Hi" print(f"{word:<5}") # left align print(f"{word:>5}") # right align print(f"{word:^5}") # center
Out:
Hiโโ โโHi โHiโ โ โ๏ธ Numbers also need some space ๐
In:
value = 12.5 print(f"{value:8.2f}") # space of width 8, 2 decimal places
Out: 12.50
โ ๐ ๏ธ Shortcut to debug
In:
x = 42 # normally you would write: print("x =", x) # with f-strings just write {x=} inside # automatically shows variable name and its value, great for many variables print(f"{x=}")
Out: x=42
โญ๐ฃ There is much more which we will discuss in future issues. That's it for today! If you liked it, please share it with anyone who will find it useful ๐ผ๐ผ โ |
Beginner to Expert in Python in just 5 minutes
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. We all agree that dashboards, emails, logs should read like English. Meaning 1.2 million beats 1234567 and 3.1M is more readable than 3145728. Stakeholders scan - and they get pissed if they have to parse messy numbers ๐ญ๐ญ So today, we explore a third-party humanize library. Format numbers, file sizes, and dates/times into human-readable text. Mini exercise at the end. ๐ฅ Ready, Get, Set, Go! Install and...
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Amazon reviews, chats and social media text (posts, comments, stories etc) are full of emojis ๐ฅ๐ฅ Great for expression! But give a hard time for text interpretation (Amazon, Meta would be going crazy). That is where Python's demoji library comes in. From a given text you can: Extract emojis Remove them if needed Replace with textual meaning Understand sentiment and what not... Let's begin!! Install demoji...
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. We all know Pythonโs basic container data types โ lists, dictionaries, tuples, sets. "Container" is just a data type that holds multiple items instead of single values. However, many times basics are not enough. Like if you want count of different items in the list you will run a loop like below. In: fruits = ["apple", "banana", "apple", "cherry"] counts = {} for item in fruits: counts[item] =...