Python Logo

Python 3 Strings

Key Terms

String
A finite sequence of characters.
Escape Character
A special character that is used to treat a character immediately after it differently than it normally would.

Declaring a String

Declaring a string in Python is simple - enclose the sequence of characters (i.e. text) in between single or double quotes. as shown below:


                    'This is a string.'
                    "This is also a string."
                

However, most of the time you want either want a string as an input to a print statement or stored in a variable.

Printing Strings to the Console

The print() function.

Outputting text to to the console will be one of the most common tasks you will be doing in learning any programming language. In Python you can "print" to the console by using the print function.


                        print('Hello World!')
                    

                        Hello World!
                    

Notice how the quotations around the string are not printed to the console.

Escaping Special Characters

Printing Single and Double Quotes

At some point, you will come across an error that occurs when you try to print a string that contains the same type of quote (double or single) you used to declare the string.


                    # Used a single quote to declare the string and in the word "won't"
                    print('This won't print!')
                    

                    File "main.py", line 1
                        print('This won't print!')
                                        ^
                    SyntaxError: invalid syntax
                    

Notice that there is a caret (^) in the character immediately following the single quote. This is the exact place where the Python interpreter noticed that there was something wrong. In basic terms, the interpeter doesn't know what to do with the character right after the quotation because it thinks you ended your string after the "n". We can fix this problem by using an escape character, \, the backslash. Escaping the single quote with the \ allows us to print the single quote to the console as shown below:


                    # Used the escape character (\) to print the single quote in "won't"
                    print('This won\'t print!')
                    

                    This won't print!
                    

Another use case for this is when you want to print out double quotes and you declared your string using double quotes as well. Notice this isn't a problem if you enclosed your string with single quotes since you don't have to escape the double quotes.


                    # Escaped the double quotes
                    print("My favorite song is \"One\" by Metallica.")
                    # Not necessary if you used single quotes for the string.
                    print('My favorite song is "One" by Metallica')
                    

                    My favorite song is "One" by Metallica.
                    My favorite song is "One" by Metallica.
                    

Printing Newlines

Sometimes you want to have an indentation in a string or an empty line between pieces of text. Let's see what happens if you try to do that:


                    print('This is line one.
                    This is line two.')
                    

                    File "main.py", line 1
                    print('This is line one
                                          ^
                                SyntaxError: EOL while scanning string literal
                    

The error occurs right before the new line was made. The interpreter spits out the error SyntaxError: EOL while scanning string literal, where EOL stands for "End of Line". It's obvious that the issue is the line inserted. We can display new lines with the newline escape, \n. If we insert this special sequence of characters then the print function will insert a new line for every \n it sees in the string.


                        print('This is line one.\nThis is line two.')
                        

                        This is line one.
                        This is line two.
                    

Conclusion

In this section we have learned the basic idea of what a string is and how to print strings to the console. Printing strings to the console is one of the most common things you will do for user interaction as well as debugging. In the next section we will learn about another basic data type in Python: Lists.