AppDividend
Latest Code Tutorials

What is Python 0 in String Formatting

0

Python utilizes zero-based indexing, which means that the first item has an index 0, the second item has index 1, and so on. Python String has an inbuilt method called index() that returns the substring index inside the string if found.

What is Python 0

Python 0 is an indicator to the format method that you need to be replaced by the first (index zero) parameter of format. It is used to execute a string formatting operation. The formatted string argument contains a literal text or replacement fields delimited by braces { }.

Python string format

Python string format() is an inbuilt method that formats the specified value(s) and inserts them inside the string’s placeholder.

Example of Python 0

Let’s format a string using the format() method and use the { 0 } as a placeholder and then replace the actual value. 

data = "11 + 19 = {0}".format(30)
print(data)

Output

11 + 19 = 30

You can see that { 0 } has been replaced with 30 in the final output. The placeholder is defined using curly brackets: { }. The placeholders can be identified using named indexes {name}, numbered indexes {0}, or even empty placeholders { }.

Formatting multiple values in Python String

You can format the multiple values in the string by using numbered indexes in Python. In the above example, we have used { 0 } because there was only one value to work with, but if you have multiple values, then you can use { 1 }, { 2 },…,{ n } gradually. 

So, { 0 } is the first argument, { 1 } is the second, { 2 } is the third argument, and so on.

tmp = "{0}'s Trailer is releasing {1}".format("Mortal Kombat", "Today")
print(tmp)

Output

Mortal Kombat's Trailer is releasing Today

You can see that each replacement field contains either the numeric index of a positional argument or the name of a keyword argument.

The format() function returns a copy of the formatted string where each replacement field is replaced with the string value of the corresponding argument.

Python 3 f-Strings

Python f String is an improvement over previous formatting methods. The f-strings are string literals with an f at the starting and curly braces enclosing the expressions replaced with their values.

Syntax

The syntax is similar to the one you used with string.format() but less messy.

print(f'{}')

As you can see, this prefixes the string constant with the letter “f“—hence the name “f-strings.” This new formatting syntax is powerful.

Example

thunder = 'Raiden'
human = 'Sonya'
print(f'MK, {thunder} and {human}')

Output

MK, Raiden and Sonya

You can see that formatted string literals are a Python parser feature that transforms the f-strings into a series of string constants and expressions. They then get joined up to construct the final string.

That is it for Python 0 in String Formatting.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.