I know of f-strings, but what are r-strings? Are there others?

Question:

I started learning python for the first time in an accelerated course on data science a few weeks ago and we were introduced early on to f-strings.

The simple code:

name = 'Tim'
print(f'There are some who call me {name}...')

outputs the string “There are some who call me Tim…”

Through my browsing of various packages out of curiosity, I came upon pages like this one detailing a function you can call in matplotlib to render $LaTeX$-like expressions within the generated images. In the example code they use something similar to f-strings but with an r instead of an f.

import matplotlib.pyplot as plt
plt.title(r'$alpha > beta$')
plt.show()

The resulting (otherwise empty) graph has a title using text which has been formatted similarly to how one would expect using MathJax or $LaTeX$ with a greek character alpha and a greek character beta.


My questions are the following:

What precisely is an r-string and how does it compare to an f-string? Are r-strings specifically used for matplotlib’s mathtext and usetex?

Apart from f-strings and r-strings, are there any other notable similar string variants or alternates that I should familiarize myself with or be made aware of?

Asked By: JMoravitz

||

Answers:

  1. An r-string is a raw string.
  • It ignores escape characters. For example, "n" is a string containing a newline character, and r"n" is a string containing a backslash and the letter n.
  • If you wanted to compare it to an f-string, you could think of f-strings as being "batteries-included." They have tons of flexibility in the ability to escape characters and execute nearly arbitrary expressions. The r-string on the other hand is stripped down and minimalist, containing precisely the characters between its quotation marks.
  • As far as actually using the things, typically you would use an r-string if you’re passing the string into something else that uses a bunch of weird characters or does its own escaping so that you don’t have to think too hard about how many backslashes you really need to get everything to work correctly. In your example, they at least needed r-strings to get the a bit working correctly without double escapes. Note that '$\alpha > \beta$' is identical to r'$alpha > beta$'.
  1. Since you’re using f-strings, I’ll assume you have at least Python 3.6. Not all of these options are supported for older versions but any of the following prefixes are valid in Python 3.6+ in any combination of caps and lowers: r, u, f, rf, fr, b, rb, br
  • The b-strings are binary literals. In Python 2 they do nothing and only exist so that the source code is compatible with Python 3. In Python 3, they allow you to create a bytes object. Strings can be thought of as a view of the underlying bytes, often restricted as to which combinations are allowed. The distinction in types helps to prevent errors from blindly applying text techniques to raw data. In Python 3, note that 'A'==b'A' is False. These are not the same thing.
  • The u-strings are unicode literals. Strings are unicode by default in Python 3, but the u prefix is allowed for backward compatibility with Python 2. In Python 2, strings are ASCII by default, and the u prefix allows you to include non-ASCII characters in your strings. For example, note the accented character in the French phrase u"Fichier non trouvé".
  • In the kind of code I write, I rarely need anything beyond r, u, f, and b. Even b is a bit out there. Other people deal with those prefixes every day (presumably). They aren’t necessarily anything you need to familiarize yourself with, but knowing they exist and being able to find their documentation is probably a good skill to have.

Just so that it’s in an answer instead of buried in a comment, Peter Gibson linked the language specification, and that’s the same place I pulled the prefix list from. With your math background, a formal language specification might be especially interesting — depending a little on how much you like algebra and mathematical logic.

Even if it’s just for a semantically trivial language like Forth, I think many programmers would enjoy writing a short interpreter and gain valuable insight into how their language of choice works.

Answered By: Hans Musgrave
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.