When declaring a string variable, we can have
a = 'hello'where we start with a single quote and end with a single quote. What if we would like to have a string
Hi, I'm a programmer? This string already contains a single quote. This can be a problem as python expects a single quote at the end of the string, not in the middle. There are several options to avoid this issue:
- We can avoid starting the string with a single quote and use
"instead. Yet, this is not a great solution as the string might contain a symbol
"as well (
A really “good” product).
- We can use an escape character
\which is specifically designed for these situations.
\escapes any character in the string that comes right after it. Our string declaration would become
a = 'Hi, I\'m a programmer'. This tells Python to treat the middle
'as a simple symbol, not an end of a string.
Here are several popular special characters in Python:
A New line (enter)
Given a string that has 5 characters, print them separated by a tab.
Tip: You can use f-strings when printing.
Time limit: 1 seconds
Memory limit: 512 MB
Output limit: 1 MB