alternative
  • Home (current)
  • About
  • Tutorial
    Technologies
    C#
    Deep Learning
    Statistics for AIML
    Natural Language Processing
    Machine Learning
    SQL -Structured Query Language
    Python
    Ethical Hacking
    Placement Preparation
    Quantitative Aptitude
    View All Tutorial
  • Quiz
    C#
    SQL -Structured Query Language
    Quantitative Aptitude
    Java
    View All Quiz Course
  • Q & A
    C#
    Quantitative Aptitude
    Java
    View All Q & A course
  • Programs
  • Articles
    Identity And Access Management
    Artificial Intelligence & Machine Learning Project
    How to publish your local website on github pages with a custom domain name?
    How to download and install Xampp on Window Operating System ?
    How To Download And Install MySql Workbench
    How to install Pycharm ?
    How to install Python ?
    How to download and install Visual Studio IDE taking an example of C# (C Sharp)
    View All Post
  • Tools
    Program Compiler
    Sql Compiler
    Replace Multiple Text
    Meta Data From Multiple Url
  • Contact
  • User
    Login
    Register

Python - String Method - format() Tutorial

It will format the specified values and insert them inside a string placeholder. the placeholder will be defined in the form of curly bracket {}.

format() method will take any number of parameters. It is divided into two types of parameters i.e

Positional Parameter

Here the list of parameters can be accessed in an expression by using an index in placeholder {} or directly by sequential order. Hence, It is called a positional parameter format().

We can also modify the order of a variable in an expression by using indexing starting from 0 and can also change the format of the required value.

For example, c= 3847.0483773 is formatted in the form of 11.3f. where f represents format is dealing with float value. 3 define the number of digits after ".", i.e it will take 3847.048. 11 define the width the number 3847.048 can take. 3847.048 has current width of 8 including ".".  remaining 3 (11-8) will be taken as padding(space) in front of number 3847.048 (i.e "   3847.048")

Example-

a="Python"
b="Fresherbell"
c= 3847.0483773
# By Default Order
print('Hello {}, Hello {}, Value of c is {}'.format(a,b,c))
# We can also change the order using indexing
print('Hello {1}, Hello {0}, Value of c is {2:11.3f}'.format(a,b,c))

Output-

Hello Python, Hello Fresherbell, Value of c is 3847.0483773
Hello Fresherbell, Hello Python, Value of c is    3847.048

 Keyword Parameter

Here the list of parameters(i.e key-value pair) can be accessed in an expression by using a key in placeholder {}. And in the format() method we can define the key-value pair. Hence, It is called a keyword parameter format().

We can also change the format of the required value.

Example-

a="Python"
b="Fresherbell"
c= 3847.0483773
# By Default Order
print('Hello {name2}, Hello {name1}, Value of c is {value:11.3f}'.format(name1 = b,name2 = a,value = c))

Output-

Hello Python, Hello Fresherbell, Value of c is    3847.048

 

Python

Python

  • Introduction
  • Installation and running a first python program
    • How to install Python ?
    • Running 1st Hello World Python Program
    • How to install Pycharm ?
  • Things to know before proceed
    • Escape Characters/Special Characters
    • Syntax ( Indentation, Comments )
    • Variable
    • Datatype
    • Keyword
    • Literals
    • Operator
    • Precedence & Associativity Of Operator
    • Identifiers
    • Ascii, Binary, Octal, Hexadecimal
    • TypeCasting
    • Input, Output Function and Formatting
  • Decision control and looping statement
    • if-else
    • for loop
    • While loop
    • Break Statement
    • Continue Statement
    • Pass Statement
  • Datatype
    • Number
    • List
    • Tuple
    • Dictionary
    • String
    • Set
    • None & Boolean
  • String Method
    • capitalize()
    • upper()
    • lower()
    • swapcase()
    • casefold()
    • count()
    • center()
    • endswith()
    • split()
    • rsplit()
    • title()
    • strip()
    • lstrip()
    • rstrip()
    • find()
    • index()
    • format()
    • encode()
    • expandtabs()
    • format_map()
    • join()
    • ljust()
    • rjust()
    • maketrans()
    • partition()
    • rpartition()
    • translate()
    • splitlines()
    • zfill()
    • isalpha()
    • isalnum()
    • isdecimal()
    • isdigit()
    • isidentifier()
    • islower()
    • isupper()
    • isnumeric()
    • isprintable()
    • isspace()
    • istitle()
  • Python Function
    • Introduction
  • Lambda Function
    • Lambda Function
  • Python Advanced
    • Iterators
    • Module
    • File Handling
    • Exceptional Handling
    • RegEx - Regular Expression
    • Numpy
    • Pandas

About Fresherbell

Best learning portal that provides you great learning experience of various technologies with modern compilation tools and technique

Important Links

Don't hesitate to give us a call or send us a contact form message

Terms & Conditions
Privacy Policy
Contact Us

Social Media

© Untitled. All rights reserved. Demo Images: Unsplash. Design: HTML5 UP.