DEV Community

Cover image for Quark’s Outlines: Python Special Characters
Mike Vincent
Mike Vincent

Posted on

Quark’s Outlines: Python Special Characters

Overview, Historical Timeline, Problems & Solutions

An Overview of Python Special Characters

What is a special character in Python?

Python reads your code one character at a time. Some characters do more than just show text or numbers. These are special characters. A special character has meaning in the structure or grammar of Python code.

Some special characters mark the start or end of a string. Others begin a comment or join lines. They are not used for math or logic, but Python needs them to understand how the code is built.

The four main special characters in Python are:

'   "   #   \
Enter fullscreen mode Exit fullscreen mode

Each one shapes how Python reads what comes next.


What do single quotes ' do in Python?

Python uses single quotes to mark the start and end of a string. A string is a piece of text. If you begin a string with a single quote, you must end it with a single quote.

Python lets you create strings using single quotes.

word = 'hello'
Enter fullscreen mode Exit fullscreen mode

If you need to put a single quote inside the string, you can use a backslash to escape it:

word = 'It\'s good'
Enter fullscreen mode Exit fullscreen mode

You can also use double quotes to avoid escaping:

word = "It's good"
Enter fullscreen mode Exit fullscreen mode

The single quote is not part of the text value. It tells Python where the string begins and ends.


What do double quotes " do in Python?

Python also lets you write strings with double quotes. This works the same as single quotes. It gives you another option when your text contains one type of quote mark.

Python lets you create strings using double quotes.

greeting = "hello"
Enter fullscreen mode Exit fullscreen mode

If your text includes a double quote, escape it with a backslash:

quote = "She said, \"Hi!\""
Enter fullscreen mode Exit fullscreen mode

Or use single quotes for the string:

quote = 'She said, "Hi!"'
Enter fullscreen mode Exit fullscreen mode

Python treats 'hello' and "hello" the same. Choose the one that fits best.


What does the hash symbol # do in Python?

The hash mark # begins a comment. A comment is a part of code that Python ignores. You use comments to explain what the code does.

Python lets you write comments using #.

# This is a comment
x = 5  # Set x to 5
Enter fullscreen mode Exit fullscreen mode

Everything after # on the same line is ignored by Python. It does not affect the program. Use comments to describe your code for yourself and others.


What does the backslash \ do in Python?

The backslash is an escape character. It changes the meaning of the character that comes next. It is also used to continue a line of code.

Python lets you escape characters and join lines using \.

text = "Line1\nLine2"
Enter fullscreen mode Exit fullscreen mode

Here, \n means a newline character. Other escapes include \t for tab, \\ for a backslash, and \" or \' for quote marks.

You can also use \ at the end of a line to join two lines into one:

total = 1 + 2 + \
        3 + 4
Enter fullscreen mode Exit fullscreen mode

Backslash is a control character. Use it carefully. One backslash changes how Python reads the next character.


A Historical Timeline of Python Special Characters

Where do Python’s special characters come from?

Python’s special characters come from older languages, shell scripts, and formal language design. They help Python understand structure and meaning in source code. This timeline shows how these characters became part of Python.


People invented control characters

1960 — Quotes for strings — ALGOL used ' and " to define string literals.
1963 — Hash for comments — BCPL and later shell scripts used # to mark comments.
1965 — Backslash escapes — C introduced \ to insert newlines, tabs, and quotes in strings.

People designed Python's grammar

1989 — Simple string syntax — Python adopted both single and double quotes from C and ABC.
1991 — Comment with hash — Python 0.9.0 used # for comments and \ for line continuation.
2000 — Raw strings — Python 2.0 added r"..." to show that backslashes are literal.
2008 — Triple quotes — Python 3 formalized ''' and """ for multi-line strings.


Problems & Solutions with Python Special Characters

How do you use Python’s special characters the right way?

Special characters help Python know where strings, comments, and line breaks are. These examples show how to use them clearly and avoid common mistakes.


Problem 1: You want to store a sentence with a quote mark

Problem: You need to write the sentence He said, "Yes" as a string. But quotes inside quotes can confuse Python.

Solution: Use single quotes outside, or escape the double quote with a backslash.

Python lets you write strings with embedded quotes.

line = 'He said, "Yes"'
line = "He said, \"Yes\""
Enter fullscreen mode Exit fullscreen mode

Both ways work. You can choose the style that needs less escaping.


Problem 2: You want to explain what a line of code does

Problem: You wrote a complex line of code and want to leave a note for yourself or others.

Solution: Use # to begin a comment.

Python lets you add notes using the hash symbol.

x = 42  # This stores the answer
Enter fullscreen mode Exit fullscreen mode

Comments make your code easier to understand later. They are not run by Python.


Problem 3: You want to include a tab or newline inside a string

Problem: You need your text to span two lines or use tabbed columns.

Solution: Use escape sequences like \n and \t inside a string.

Python lets you control layout inside strings using escapes.

text = "Item\tPrice\nEggs\t$2.00"
print(text)
Enter fullscreen mode Exit fullscreen mode

This prints:

Item    Price
Eggs    $2.00
Enter fullscreen mode Exit fullscreen mode

Escapes make your string output look the way you want.


Problem 4: You want to write a long line of code but keep it readable

Problem: You are adding many numbers or calling a function with many arguments. The line gets too long.

Solution: Use a backslash to continue the line.

Python lets you break long lines using a backslash.

total = 1 + 2 + 3 + \
        4 + 5 + 6
Enter fullscreen mode Exit fullscreen mode

Python joins both lines as if they were one. You can keep your code neat and under 80 characters.


Like, Comment, Share, and Subscribe

Did you find this helpful? Let me know by clicking the like button below. If you liked the story, share it with a friend. I'd love to hear your thoughts in the comments below! Thanks for reading!


Mike Vincent is an American software engineer and writer based in Los Angeles. Mike writes about technology leadership and holds degrees in Linguistics and Industrial Automation. More about Mike Vincent

Top comments (5)

Collapse
 
michael_liang_0208 profile image
Michael Liang

Great post for python developers!

Collapse
 
mike-vincent profile image
Mike Vincent

Michael, I really appreciate the comment on my story for software engineers.I love the little-details about programming languages. Glad you enjoyed it.

Collapse
 
michael_liang_0208 profile image
Michael Liang

Python is very easy to learn for developers.
Which language is more popular in programmers, Python or Javascript?
What do you think about this?

Collapse
 
nathan_tarbert profile image
Nathan Tarbert

Pretty cool, I always need reminders on the little things like this

Collapse
 
mike-vincent profile image
Mike Vincent

Nathan, thanks so much for reading Quark's Outlines. I'm doing a deep dive into every sentence in the Python Reference Manual. Glad you found it it useful engineering knowledge!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.