๐ŸŽธ Play with text in Python


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:

  • Everything inside {} is evaluated and inserted.
  • Combine text and numbers with ease - no need to use + or commas!
  • Build dynamic, readable strings in one line!

โ€‹

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 ๐Ÿผ๐Ÿผ

โ€‹

Pandas Daily

Beginner to Expert in Python in just 5 minutes

Read more from Pandas Daily

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] =...