Welcome back to Pandas Daily! Today, master Python in 7 snippets. β Yesterday, we explored "f-strings", a MUST KNOW modern and clean way to insert text and format strings. However, you would also come across .format() , a previous version still used by many (for sure by my old boss :/ ). Note: Do go through the pervious post to better understand .format() β π₯ Pass values using {}
In:
name = "Sam" # put the value into { } print("Hello, {}".format(name))
Out: Hello, Sam
β π Multiple curlies {} to add multiple values
In:
name = "Sam" marks = 88 # values fill the { } in order print("{} scored {} marks".format(name, marks))
Out: Sam scored 88 marks
β π§ Control which value needs to be placed where
In:
# {1} uses the second value, {0} uses the first print("{1} comes after {0}".format("A", "B"))
Out: B comes after A
β πͺΆ Define variable directly in .format()
In:
# use labels inside { } and pass values by label print("Name: {n}, Age: {a}".format(n="Sam", a=25))
Out: Name: Sam, Age: 25
β πΉοΈ Pad numbers with zeros
In:
num = 5 # width 3, pad with zeros on the left print("{:03}".format(num))
Out: 005
β π€ Add comma for large numbers
In:
num = 1000000 print("{:,}".format(num))
Out: 1,000,000
β π‘ Align text left, right and center
In:
print("{:<5}".format(word)) # left print("{:>5}".format(word)) # right print("{:^5}".format(word)) # center
Out:
Hi Hi Hi βπ£ 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] =...