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.
Table of Contents
Common Escape Characters
Escape Sequence | Description | Example |
\\ | Backslash | “This is a backslash: \\” |
\’ | Single quote | “It’s a nice day” |
\” | Double quote | “He said, \”Hello!\”” |
\n | Newline | “Line1\nLine2” |
\t | Tab | “Column1\tColumn2” |
\r | Carriage return | “Hello\rWorld” |
\b | Backspace | “Hello\bWorld” |
\f | Form 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

Check out our Trending Courses Demo Playlist
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 |
Kick Start Your Career With Our Data Job
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
Most Commented