What to Use string.Format() or Percentage (%) in Python

In this quick tutorial, you’ll get to know which method to choose between format() function or percentage (%) to use for string formatting in Python.

Like the most programming languages, Python too provides a mechanism to format a string. For example, in ‘C’ language, the ‘%’ sign (a.k.a. modulus operator) acts as a format specifier and so does it in Python.

However, Python also has an alternate way for string formatting via the format() function. We will explore the key differences between the ‘%’ operator and the string.format() function in this post.

Python String.Format() Or Percentage (%) for Formatting

Format or percentage for string formatting in Python

Let’s first dig into the percentage (%) sign and see what it does.

Using Percentage (%) to Format Strings

If you like to perform some simple string formatting, then try using the ‘%’ operator. It is a pretty old style and will remind you of the C programming language.

Python also adheres to this type of formatting and can format values of all its data types with the % sign.

Check out the below example.

# Percentage (%) demo to format strings
subject = "Data Science"
language = "Python"
output_str = 'I am studying %s and using %s as the programming language.' % (subject, language)
print (output_str)

You should note here that you need to postfix ‘%’ with a type-specific char to indicate the type of data to print. It signifies whether to substitute a number or a string.

# Percentage (%) demo to format both strings and integer types
>>> strHundred = "Hundred"
>>> intHundred = 100
>>> "%d means %s" % (intHundred , strHundred)

'100 means Hundred'

If you don’t provide an integer type value against the ‘%d’, then Python will raise the TypeError.

The primary problem with the C printf style formatting is that it limits the parameters to be passed. Also, most programmers don’t find as intuitive as the new features in Python.

Using Format() for String Formatting

It was one of the newest features which got introduced in Python 3 for simplifying string formatting. It used a mini-language that provided ease of use and more options.

Please note that its creators also ported it for Python 2.6, one of the most used versions of Python till date.

The format function requires curly brackets as a replacement for % modifiers. It replaces the first occurrence of curly brackets with the value provided as a parameter to the format function.

Check out the below example.

# String.Format() demo to format strings
>>> 'The site {} helped me learn Python.'.format('TechBeamers')

'The site TechBeamers helped me learn Python.'

See another example to discover a potential difference between the string.format() function and the % operator.

# String.Format() demo to format multiple strings and an integer
>>> 'The site {} helped me learn {} basics in just {} days.'.format('TechBeamers', 'Python', 7)

'The site TechBeamers helped me learn Python basics in just 7 days.'

It is quite evident from the above examples that the format function completely frees you from hard-coding the type of data to print and lets you focus on the formatting.

For more detailed info on this topic, read this comprehensive tutorial – Python String Formatting