7.6. Special Characters

Aside from letters, numbers, and symbols, there is another class of characters we can use in strings, known as special characters. These characters involve special codes that all begin with a \ (backslash). Special characters allow us to include control characters, whitespace characters, and items that do not appear on our keyboards (like shapes or emojis).

7.6.1. Newline and Tab

Two commonly used special characters are \n and \t, which are the newline and tab characters, respectively. A newline represents tapping the Return or Enter key while typing.

Example

print("A message,\nbroken across lines,\n\tand indented.")

Console Output

A message,
broken across lines,
   and indented.

Try It!

Modify the code in the editor below to produce this output:

Use newline
   and tab
      characters to
         create this
      output with
   a single
print statement.

7.6.2. Other Characters

We can also add characters to a string that do not appear on all keyboards. These Unicode characters use combinations of the form \uXXXX, where the four Xs are numbers or letters that stand for a particular symbol. This allows us to use character sets that don’t use the Latin letters (A-Z), such as Greek, Cyrillic, and Arabic, as well as a wide array of non-letter symbols.

Example

print('\u25E8     \u26BD     \u26A1')

Console Output

◨     ⚽     ⚡

For a complete listing of codes, check out this Unicode table.

7.6.3. Check Your Understanding

Question

Which of the options below prints 'Special' and 'characters' on separate lines?

  1. print('Special\ncharacters')
  2. print('Special/ncharacters')
  3. print('Special', 'characters')
  4. print('Special\tcharacters')
  5. print('Special/tcharacters')