Wednesday, February 12, 2014

Python triple-quoted strings vs. raw strings

Python lets you include text as strings in a number of ways, but picking the right one is important. There are two specific types of strings that I get confused and occasionally need a little reference to sort out: triple-quoted strings and raw strings.

A triple-quoted string has three quotation marks and looks like this:

TRIPLE = """first\nsecond

whereas a raw string has a preceding letter r and looks like this:

RAWSTRING = r"I want some \nicely\ formatted text"

The triple-quoted string preserves everything in it, including newline characters, and it interprets the backslashes as "escaped" characters like in regular strings. It will include anything but another triple quote, which it interprets as the end. If you printed TRIPLE it would look like this:


Raw strings work a little differently. A raw string won't let you break the string over a line in the middle, and it doesn't interpret escaped characters. It will not convert the "\n" to a newline like ordinary strings or triple-quoted strings will. So if you printed RAWSTRING you would see this:

I want some \nicely\ formatted text

Triple-quoting is very convenient for copying data from somewhere and pasting it into a string when you want to preserve the newlines. Raw srtings are useful for code-related things that may contain backslashes for other reasons, such as regular expressions.

I can use a triple-quoted string to pull the list of users directly from an email and stick it into a string in a Python script, and then I can use a raw string to create a regular expression to pull the usernames out:

#!/usr/bin/env/ python3

import re

DATA = """Holly Martins (HMARTINS)
Anna Schmidt (ASCHMIDT)
Harry Lime (HLIME)"""

REGEX = r"\s\((\w+)\)"

names = re.findall(REGEX,DATA)
for name in names:




  1. One more thing: a triple quoted string can also be raw, as in


    This should treat the backslashes within the triple quoted string as ordinary characters again, and is in fact useful for forming VERBOSE regular expressions, with comments embedded right inside the regular expression. (See document for the re package.)

  2. A computer designed to be used by a single person is defined as a personal computer (PC). While a Mac is a personal computer, systems running the Windows OS are considered PCs by most people. useful source

  3. Mmm.. good to be here in your article or post, whatever, I think I should also work hard for my own website like I see some good and updated working in your site. click here

  4. It was a decent post to be sure. I completely delighted in understanding it in my lunch time. Will definitely come and visit this blog all the more frequently. Much obliged for sharing. visita il sito

  5. Just pure brilliance from you here. I have never expected something less than this from you and you have not disappointed me at all. I suppose you will keep the quality work going on. klik voor meer

  6. Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? mejores vpn

  7. I genuinely believed you would probably have something useful to say. All I hear is a bunch of whining about something that you can fix if you were not too busy looking for attention. After all, I know it was my choice to read..

  8. Always so interesting to visit your site.What a great info, thank you for sharing. this will help me so much in my learning getmoreprivacy

  9. There are extra fringe gadgets which can associate with the Motherboard also through an assortment of information ports which are associated with the Motherboard, for example, a printer, computerized camera, amplifier, and even a HDTV. high processor computer

  10. Your blog provided us with valuable information to work with. Each & every tips of your post are awesome. Thanks a lot for sharing. Keep blogging,

  11. This is a smart blog. I mean it. You have so much knowledge about this issue, and so much passion. You also know how to make people rally behind it, obviously from the responses. privacyonline

  12. Do I buy an Apple or another computer? The other computer main selections are Windows 7 operating system or Android operating system computers. Best Computer For College student 2019

  13. It is easier, though, if you are keeping your computer because if you get a new PC, you will have to pay someone to transfer that information to a new PC. reset windows 10 password

  14. I adore all the strings, My partner and i savored, I'd personally love far more details using this, for the reason that it really is really pleasurable., Enjoy it designed for offering. 144hz led monitor

  15. Laptops, also known as notebooks, are small-sized portable computers can be placed on the lap and used there. Read All in one computer's reviews

  16. Physically limiting the access to a computer so that computer security will not be compromised by those who are granted access to use it.

  17. You will then need to make a quick online purchase for the service you are requesting, such as for a one-time repair or a recurring monthly payment for an unlimited amount of repairs, usually for a minimum 1 year contract. 24 hour computer repair

  18. Albeit exceptionally little in size, such laptops have generally excellent figuring execution, which makes them a decent bundle to have.Best Laptops For College 2019: Expert's Advice

  19. Wonderful article. Fascinating to read. I love to read such an excellent article. Thanks! It has made my task more and extra easy. Keep rocking.

  20. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. hoverwatch discount

  21. Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info. Laptop

  22. There are many competing hardware and software products for Windows 7 computers. Windows 7 computers are the most malware, spyware, and virus attacked computers. Because there are more Windows computers sold than any other computers, Windows computers are the biggest target to attack.usb c charger

  23. Outstanding article! I want people to know just how good this information is in your article. Your views are much like my own concerning this subject. I will visit daily your blog because I know. It may be very beneficial for me. Extratorrent