😰 We can't escape Python's escape sequences


Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python.

​

I am sure when you print C:\new_folder your code breaks due to \. Or you wonder how to print single (It's) or double ("Hello") quotes.

Enter Escape Sequences, tiny backslash commands in Python that fix all of this.

πŸ‘‰ Most common uses:

  • Format using new lines
  • Print tabs
  • Single or double quotes in text
  • Write file paths safely
  • Print symbols like Ο€ or❀️

​

Let's Begin...

​

πŸ‘‡ \n Print in next line - the one we all know

In: print("Hello\nWorld")
Out:
Hello
World

​

πŸ“… \t Print in tabular format

In:
print("Name\tAge")
print("John\t25")
Out:
Name    Age
John    25

​

🌟 \ Backslash for windows file paths

In: print("C:\Users\Admin")
Out:
C:\Users\Admin

​

πŸͺΆ ' Single and '' Double quotes

In:
print('It\'s Python time')
print("He said \"Python is fun\"")
Out:
It's Python time
He said "Python is fun"

​

π„ž \u Unicode for symbols and emojis

In:
print("Ο€")
print("❀")
Out:
Ο€
❀

​

❌ r" " Ignore escape sequences in strings

In:
print("C:\new_folder")
print(r"C:\new_folder")
Out:
C:
ew_folder
C:\new_folder

​

βœ’οΈ \r Rewrite on same line

First "Loading" will print till process is running, and will be replaced by "Done"

In:
import time

print("Loading...", end="\r")
time.sleep(1)
print("Done! ")
Out: Done!

β­πŸ“£ That's it for today! If you liked it, please share it with anyone who will find it useful and share your feedback below 🐼

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. Think about the time you waste right-clicking and saving course lectures on YouTube one-by-one. Inefficient process, can cause errors and you lose focus. pytubefix eliminates that bottleneck. Today is day 3 of mastering YouTube with Python. You already know how to get stats of a single video; or download them in various formats. Now time to level up with playlists. Pytubefix has a Playlist object that...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Yesterday we introduced pytubefix - a powerful module to access YouTube. We downloaded a video as well. Cool, but basic. What if you want 1080p instead of 480p. Or prefer WebM over MP4. To answer all this - let's cover downloads in depth today. 🀫 I am 100% sure you can charm recruiters or your colleagues as they won't know this! πŸ“₯ Code Recap: How to download YouTube video In: # Import library from...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. We all watch YouTube - almost daily. Only a few however know how to control it with Python. Today we will use pytubefix - a Python library that provides access to YouTube. Know video stats (# views, upload date) or download them, all in a line of code ⚠️ Note: Respect YouTube’s terms and download only content you own or have permission for. πŸ“¦ Install pytubefix In: pip install pytubefix Out: 🏑 Basic...