Mastering Escape Characters in Python

escape_characters_in_python

Escape characters are used in strings to include special characters that are difficult to type directly. They start with a backslash (\) and allow you to add characters like tabs, newlines, or quotes in your string.

Common Escape Characters

Escape SequenceDescriptionExample
\\Backslash“This is a backslash: \\”
\’Single quote“It’s a nice day”
\”Double quote“He said, \”Hello!\””
\nNewline“Line1\nLine2”
\tTab“Column1\tColumn2”
\rCarriage return“Hello\rWorld”
\bBackspace“Hello\bWorld”
\fForm feed“Hello\fWorld”

Examples:

1. Backslash and Quotes

txt = "He said, \"Hello, it's a nice day!\""
print(txt)  # Output: He said, "Hello, it's a nice day!"

2. Newline and Tab

multiline_txt = "Line1\nLine2"
tabbed_txt = "Column1\tColumn2"
print(multiline_txt)
print(tabbed_txt)

3. Carriage Return

txt = "Hello World!\rPython"
print(txt)  # Output: Python World!

4. Form Feed and Backspace

form_feed_text = "Page1\fPage2"
backspace_text = "Hello\bWorld"
print(form_feed_text)  # Output: Page1
                       #         Page2
print(backspace_text)  # Output: HellWorld

Conclusion

Escape characters, such as newlines or quotes, make it easy to include special characters in your strings. They help in formatting text and ensuring your strings are clear and readable.

Knowledge Check

Related Article No.4

Master Python Escape Characters in 2024
Data Analytics with Power Bi and Fabric
Could Data Engineer
Data Analytics With Power Bi Fabic
AWS Data Engineering with Snowflake
Azure Data Engineering
Azure & Fabric for Power bi
Full Stack Power Bi
Subscribe to our channel & Don’t miss any update on trending technologies

Kick Start Your Career With Our Data Job

Master Fullstack Power BI – SQL, Power BI, Azure Cloud & Fabric Tools
Master in Data Science With Generative AI Transform Data into Business Solutions
Master Azure Data Engineering – Build Scalable Solutions for Big Data
Master AWS Data Engineering with Snowflake: Build Scalable Data Solutions
Transform Your Productivity With Low Code Technology: Master the Microsoft Power Platform

Social Media channels

► KSR Datavizon Website :- https://www.datavizon.com
► KSR Datavizon LinkedIn :- https://www.linkedin.com/company/datavizon/
► KSR Datavizon You tube :- https://www.youtube.com/c/KSRDatavizon
► KSR Datavizon Twitter :- https://twitter.com/ksrdatavizon
► KSR Datavizon Instagram :- https://www.instagram.com/ksr_datavision
► KSR Datavizon Face book :- https://www.facebook.com/KSRConsultingServices
► KSR Datavizon Playstore :- https://play.google.com/store/apps/details?id=com.datavizon.courses&hl=en-IN
► KSR Datavizon Appstore :- https://apps.apple.com/in/app/ksr-datavizon/id1611034268

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *