Lesson 3

Unleashing Python Strings: A Beginner's Guide to Escaping and Special Characters

Topic Overview and Introduction

Welcome! Today, we're exploring Escaping Characters and Using Special Characters in Python. Are you ready for this engaging journey through Python Strings?

Understanding Escaping Characters

Let's consider a situation where you need to write a sentence with quotes or some other special symbols inside a string. That's when 'escaping' becomes handy. In Python, a backslash (\) is used to 'escape' special characters in strings.

For example, '\"' is used in Python to denote a quotation mark inside a string, as demonstrated below:

1greeting = "The machine said: \"Hello, World!\"" 2print(greeting) # The machine said: "Hello, World!"
Exploring Special Characters

Special characters such as newline (\n), tab (\t), and the backslash itself (\\), impart entirely new functionalities to strings:

1print("This is a string\nThis is on a new line") # Prints a new line after "This is a string" 2print("Here is a\ttab space") # Prints "Here is a tab space" 3print("This is a backslash \\") # Prints "This is a backslash \"
Embedding Special Characters in Strings

By incorporating special characters in strings, you essentially enhance their expressiveness. Let's create a string, for instance, that represents a simple table:

1table = "Name\tAge\nAlice\t23\nBob\t25" 2print(table) # Outputs data like a table 3""" 4Name Age 5Alice 23 6Bob 25 7"""

You can also include internal quotes by using different types for your string and your internal quotes:

1single_quoted = 'This is a "quoted" word' # using single quotes to print double quotes inside the string 2print(single_quoted) # This is a "quoted" word 3 4double_quoted = "This is a 'quoted' word" # using double quotes to print single quotes inside the string 5print(double_quoted) # This is a 'quoted' word
Using Raw Strings to Avoid Escaping

Raw strings treat backslashes as normal characters, making them ideal for strings with paths:

1raw_path = r'C:\Users\John\Desktop' 2print(raw_path) # C:\Users\John\Desktop

As you can see, we defined a raw string by adding 'r' at the beginning.

You can also combine r and f attributed together:

1game = "MyGame" 2print(rf"Here is the {game}'s path: C\Users\John\Desktop")
Using Unicode Characters in Python Strings

Python strings can store any character, thereby enabling the use of any alphabet, symbols, or emojis:

1print("Python is as easy as αβγ") # Python is as easy as αβγ 2print("I love Python ❤️!") # I love Python ❤️!
Lesson Summary

Escaping and special characters enhance string formatting and expressiveness, and they are widely used in real-world scenarios.

Great job! You have now cleared the hurdles of escaping characters, using special characters and raw strings, and you've also had a sneak peek at Unicode in Python strings. Now, get ready for hands-on exercises to put these concepts into practice and solidify your understanding. Let's do it!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.