- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Triple Quotes in Python
Python's triple quotes comes to the rescue by allowing strings to span multiple lines, including verbatim NEWLINEs, TABs, and any other special characters.
The syntax for triple quotes consists of three consecutive single or double quotes.
Example
#!/usr/bin/python para_str = """this is a long string that is made up of several lines and non-printable characters such as TAB ( \t ) and they will show up that way when displayed. NEWLINEs within the string, whether explicitly given like this within the brackets [ \n ], or just a NEWLINE within the variable assignment will also show up. """ print para_str
Output
When the above code is executed, it produces the following result.
Note how every single special character has been converted to its printed form, right down to the last NEWLINE at the end of the string between the "up." and closing triple quotes. Also note that NEWLINEs occur either with an explicit carriage return at the end of a line or its escape code (\n) −
this is a long string that is made up of several lines and non-printable characters such as TAB ( ) and they will show up that way when displayed. NEWLINEs within the string, whether explicitly given like this within the brackets [ ], or just a NEWLINE within the variable assignment will also show up.
Raw strings do not treat the backslash as a special character at all. Every character you put into a raw string stays the way you wrote it −
Example
#!/usr/bin/python print 'C:\nowhere'
Output
When the above code is executed, it produces the following result −
C:\nowhere
Now let's make use of raw string. We would put expression in r'expression' as follows −
Example
#!/usr/bin/python print r'C:\nowhere'
Output
When the above code is executed, it produces the following result −
C:\nowhere
- Related Articles
- A number and its triple in Python
- Avoiding quotes while printing strings in Python
- What are different types of quotes in Python?
- Single quotes vs. double quotes in C or C++
- Python - Check if a number and its triple exists in an array
- What is Triple DES?
- What is the difference between single and double quotes in python?
- How to print double quotes with the string variable in Python?
- CSS quotes Property
- Finding the Largest Triple Product Array in JavaScript
- Single and Double Quotes in Perl
- Print newline in PHP in single quotes
- What are the implementation of Triple DES?
- Escaping quotes while inserting records in MongoDB?
- How to escape single quotes in MySQL?
