Ask Your Question

What is the meaning of % and when we should use it?

asked 2022-01-20 15:53:25 +0200

Arighetto gravatar image

The tutorials do not mention it.

edit retag flag offensive close merge delete


rburing gravatar imagerburing ( 2022-01-20 19:53:28 +0200 )edit

Or do you mean something like 5%2, which means the remainder upon dividing 5 by 2?

John Palmieri gravatar imageJohn Palmieri ( 2022-01-20 20:44:48 +0200 )edit

The most up-voted answer to explains two of the uses of % in Python, and another answer also discusses Jupyter/IPython magic commands.

John Palmieri gravatar imageJohn Palmieri ( 2022-01-20 20:46:28 +0200 )edit

Hilarious, I have no idea why the first one that occurred to me was formatting.

kcrisman gravatar imagekcrisman ( 2022-01-25 19:49:37 +0200 )edit

Without further information, perhaps we should close the question: too imprecise.

John Palmieri gravatar imageJohn Palmieri ( 2022-01-25 20:50:02 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2022-01-20 19:33:32 +0200

kcrisman gravatar image

I presume you are talking about string formatting. In this case, most Python tutorials will tell you how to format/replace strings inside of other strings with various variables. An example from one tutorial:

>>> name = 'Bob'
>>> 'Hello, %s' % name
"Hello, Bob"

Here is an example using interact to show computations nicely, which is a primary way to use string formatting in Sage usefully.

However, one should point out that the new-style formatting ('Hello, {}'.format(name) in the linked tutorial) is probably mostly recommended.

edit flag offensive delete link more



or simply f'Hello {name}'

Max Alekseyev gravatar imageMax Alekseyev ( 2022-01-20 20:15:17 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2022-01-20 15:53:25 +0200

Seen: 51 times

Last updated: Jan 20