Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 294
JMoravitz Avatar asked Sep 17 '18 03:09

JMoravitz


People also ask

What is r in string?

It is a one-dimensional array of characters. One or more characters enclosed in a pair of matching single or double quotes can be considered a string in R.

What two symbols does an F-string work?

The f-strings have the f prefix and use {} brackets to evaluate values. Format specifiers for types, padding, or aligning are specified after the colon character; for instance: f'{price:. 3}' , where price is a variable name.

What does r before a string do?

r means the string will be treated as raw string. See the official Python 2 Reference about "String literals": When an 'r' or 'R' prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string.

What does R in front of a string mean?

The r prefix on strings stands for “raw strings”. Standard strings use backslash for escape characters: “\n” is a newline, not backslash-n.


1 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$'.
  2. 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, 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 the answer instead of buried in a comment, @JamesGibson 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.

like image 139
Hans Musgrave Avatar answered Oct 01 '22 04:10

Hans Musgrave