How do you insert a special character in python?

I am making a set in Python to house all the symbols on my keyboard, but obviously a few pose some issues. Is there a way to get them all in there without encountering problems?

Here is my set:

symbols = {`,~,!,@,#,$,%,^,&,*,(,),_,-,+,=,{,[,},},|,\,:,;,",',<,,,>,.,?,/}

To get around commenting out most of it, since in Python # is to comment, I enclosed everything like so:

symbols = {'`','~','!','@','#','$','%','^','&','*','(',')','_','-','+','=','{','[','}','}','|','\',':',';','"',''','<',',','>','.','?','/'}

Which works for that character, but now I can already see an issue when I come across the ' and \. Is there a better way to make this set?

On this page: commenting with #, multi-line strings with """ """, printing multiple objects, the backslash "\" as the escape character, '\t', '\n', '\r', and '\\'.

Get Started

Video Summary

  • Most of the print statements in this script were commented out initially, which were uncommented throughout the video. This is the shell output.
  • As stated in earlier tutorials, the print function tells Python to immediately display a given string once the command is executed. To designate a string for the print function to display, surround it in either single-quotes (' ') or double-quotes (" "). Both options are available so you can still use quotes within your string if need be. Ex: print "how are you doin' today?"
  • If the pound symbol (#) is placed before a command or any sort of string of characters, the command will appear in red and Python will ignore it during code execution. This can be used within Python to provide helpful comments to those looking at your code, or to "turn off" certain lines of code in order to test for bugs.
  • Surrounding a string with triple double-quotes (""" """) allows you to have any combination of quotes and line breaks within a string and Python will still interpret it as a single entity.

Learn More

  • You can specify multiple strings with the print statement. Just separate them out with a comma ',', and they will be printed with a space in between:

     
    >>> print 'apple', 'orange', 'pear'
    apple orange pear 
    

  • In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.

     
    >>> print 'apple\torange'
    apple	orange 
    >>> print 'apple\norange'
    apple
    orange 
    

  • Conversely, prefixing a special character with "\" turns it into an ordinary character. This is called "escaping". For example, "\'" is the single quote character. 'It\'s raining' therefore is a valid string and equivalent to "It's raining". Likewise, '"' can be escaped: "\"hello\"" is a string begins and ends with the literal double quote character. Finally, "\" can be used to escape itself: "\\" is the literal backslash character.

     
    >>> print 'It\'s raining'
    It's raining 
    >>> 'It\'s raining'          # Same string specified differently
    "It's raining" 
    >>> print "\"hello\""
    "hello" 
    >>> print '"\\" is the backslash'   # Try with "\" instead of "\\"
    "\" is the backslash 
    

  • There are tons of handy functions that are defined on strings, called string methods. Learn about the ones on substringhood and also on case manipulation in this tutorial. This part 2 tutorial covers string methods for finding where a particular substring is located, and also for testing whether or not certain condition holds for every character.
  • Once you get comfortable with lists (upcoming), you should also check out Splitting and Joining Strings.

Explore

  • Think Python has an excellent chapter (Ch.8 Strings) devoted to strings. It gives a comprehensive overview on what one can do with this data type.

Escape sequences allow you to include special characters in strings. To do this, simply add a backslash (\) before the character you want to escape.

For example, imagine you initialized a string with single quotes:

s = 'Hey, whats up?'
print(s)

Output:

Hey, whats up?

But if you include an apostrophe without escaping it, then you will get an error:

s = 'Hey, what's up?'
print(s)

Output:

  File "main.py", line 1
    s = 'Hey, what's up?'
                   ^
SyntaxError: invalid syntax

To fix this, just escape the apostrophe:

s = 'Hey, what\'s up?'
print(s)

To add newlines to your string, use \n:

print("Multiline strings\ncan be created\nusing escape sequences.")

Output:

Multiline strings
can be created
using escape sequences.

An important thing to remember is that, if you want to include a backslash character in a string, you will need to escape that. For example, if you want to print a directory path in Windows, you'll need to escape each backslash in the string:

print("C:\\Users\\Pat\\Desktop")

Output:

C:\Users\Pat\Desktop

Raw strings

A raw string can be used by prefixing the string with r or R, which allows for backslashes to be included without the need to escape them. For example:

print(r"Backslashes \ don't need to be escaped in raw strings.")

Output:

Backslashes \ don't need to be escaped in raw strings.

But keep in mind that unescaped backslashes at the end of a raw string will cause and error:

print(r"There's an unescaped backslash at the end of this string\")

Output:

  File "main.py", line 1
    print(r"There's an unescaped backslash at the end of this string\")
                                                                      ^
SyntaxError: EOL while scanning string literal

Common escape sequences

Escape SequenceMeaning
\ Backslash (\)
' Single quote (')
" Double quote (")
\n ASCII Linefeed (adds newline)
\b ASCII Backspace

A full list of escape sequences can be found here in the Python docs.

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

How do I type a symbol in Python?

To print any character in the Python interpreter, use a \u to denote a unicode character and then follow with the character code. For instance, the code for β is 03B2, so to print β the command is print('\u03B2') . ... Useful unicode symbols in engineering..

How do you add special characters?

Click or tap where you want to insert the special character. Go to Insert > Symbol > More Symbols. Go to Special Characters. Double-click the character that you want to insert.

How do you get special characters in a string in Python?

Python Program to check special characters.
Input: "Hello!!".
Output: string is not accepted..
Input: "hello123".
Output: string is accepted..
Step 1- Import re module..
Step 2- Define a function to check for special characters..
Step 3- Create a regular expression of all the special characters..

How do you print special symbols in Python?

Use repr() to print special characters.
a_string = "\nString\n".
literal_string = repr(a_string).
print(literal_string).