What does % do to strings in Python?

All we need is an easy explanation of the problem, so here it is.

I have failed to find documentation for the operator % as it is used on strings in Python. What does this operator do when it is used with a string on the left hand side?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

It’s the string formatting operator. Read up on string formatting in Python.

format % values

Creates a string where format specifies a format and values are the values to be filled in.

Method 2

It applies printf-like formatting to a string, so that you can substitute certain parts of a string with values of variables.

# assuming numFiles is an int variable
print "Found %d files" % (numFiles, )

See the link provided by Konrad

Method 3

Note that starting from Python 2.6, it’s recommended to use the new str.format() method:

>>> "The sum of 1 + 2 is {0}".format(1+2)
'The sum of 1 + 2 is 3'

If you are using 2.6, you may want to keep using % in order to remain compatible with older versions, but in Python 3 there’s no reason not to use str.format().

Method 4

The ‘%’ operator is used for string interpolation. Since Python 2.6 the String method “format” is used insted. For details see http://www.python.org/dev/peps/pep-3101/

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply