python string Format()

In this article, you will learn total fundamental things about Python string format with real examples. So let’s started to know more about this-

Key points of Python String Format-

  • Definition of string formatting with example
  • Using process of str.format()
  • Multiple value adding process
  • Index number or positional arguments
  • Named index
  • Index Error

What is Python string Formatting 

Python formatting string is the function used to replace, substitute, and convert the string with the valid value of the final result of the string. This is the built-in function that contains Python strings. The placeholder inside the python string that defined brackets. 

Example of string formats –

“ Welcome to WigMarketing Digital Solution {}” .format(‘ value here’).

Using process of str.format()

The string formats used the methods that will scan the original elements for placeholders. It can have placeholders with index 0, 1. 

This is the most important part of python using string format methods. It is easy to use any syntax. 

Example of python string syntax methods – 

price = 50

txt = “The price is {} taka”


The output of this –

At the same points, you can add parameters inside the brackets to separate its value. Here I share displays as numbers with decimals. 

Example of parameters brackets-

price = 90

txt = “The price is {:.2f} taka”


The output of these –

Add more value at a time

When you want to add multiple strings at a time, you can add multiple strings using this method. 

Example of the multiple values of string –

quantity = 10

itemno = 500

price = 90

myorder = “I want {} pieces of item number {} for {:.2f} taka.”

print(myorder.format(quantity, itemno, price))

The output of more value –

Index number or positional arguments of python string 

Index number means a number that includes inside the curly brackets and ensures its value performs the correct placeholder. Here the value will appear in ordered directed format().

Example of index number of string 

quantity = 10

itemno = 500

price = 90

myorder = “I want {0} pieces of item number {1} for {2:.2f} taka.”

print(myorder.format(quantity, itemno, price))

The output of these –

Named index with string format

If you also want to use a name with an index number, you can do this by using the named index string. Here I include how to add a named string with your value.

Example of using named index-

myorder = “I have a {websitename}, it provides {services}.”

print(myorder.format(websitename = “WigMarketing”, services = “Digital Solution”))

The output for this-

Using process directory with the string format()

Use the directory to convert the value of inside format (). Here I share the using process of the directory with formats()

Example of the directory –

New_dict = {‘msg4’ : “Welcome”, ‘msg5’ : ‘WigMarketing’}

print (“ m [ msg4] } to the { m[ msg5] } Service area! “. format( m=new_dict))

The output of these –

Welcome to the WigMarketing Service area!

Index Error

When happening anything else with string format, here you see index error issues. You may be seen when the number of placeholders passed in a string format() doesn’t match with the parameters of the function. If you used five placeholders for Four parameters, in these points you see Index Errors.

In this article, you will be able to learn more about python string format() that can help to declare padding, center aligns formatting with the number, named, multiple values, index or positional arguments, index Errors, and more. Stay here to learn total python. Have any questions, drop a comment I will back to help you.

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!
Open chat
Hey, how can I help you?