EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Python 3 Tutorial Python 3 String Format
 

Python 3 String Format

Updated February 21, 2023

Python 3 String Format

 

 

Introduction to Python 3 String Format

Python 3 string format work by inserting the single or more placeholders and fields of replacement into a string and using the str format function. The Python format method was developed to handle sophisticated string formatting effectively. Complex variable substitutions and value formatting are supported by this method of the built-in string class. This new method of formatting is thought to be more elegant.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

What is Python 3 String Format?

  • Probably the most popular use case is simple positional formatting. If arguments of the order are unlikely to change and we have only a few parts to concatenate, this is the method to use.
  • This minimal style should only be used to format a small number of items because their name does not represent the element.
  • Values are formatted by default to fit as characters are required to express the content. However, we can specify that a value is padded as a certain length.
  • Regrettably, the default alignment varies depending on whether we are using the old or new type of formatting. The new style is left-aligned by default, whereas the old style is right-aligned by default.
  • String formatting can be done in four different ways.
    1. Using the percent operator to format.
    2. The format string method is used to format the data.
    3. F-strings are string literals that are used to format data.
    4. String Template Class Formatting.
  • Formatters work by inserting single or many placeholders and fields of replacement into a string and using the str format function.
  • The value we want to put in the placeholders concatenated with the text supplied to the format method as parameters.
  • The placeholder is the following several alphanumeric letters without gaps by enclosing them in braces. When we write $$, we get a single $.
  • Template Class in the String module enables us to develop output specification syntax that is easier to understand. The format combines $ with valid Python identifiers to create placeholder names.

How to Use Python 3 String Format?

  • The format function formats the provided value(s) and places them in the placeholder of a string.
  • Curly brackets are used to define the placeholder. Finally, it is used to return the string which was formatted.
  • The string value which we have concatenated will be passed to the method. This value will be sent through when we run the application in the exact location of our placeholder.

Below syntax shows how to use python 3 string format.

Syntax:

String.format(value1, value2, …)

Value1 and value are required; it is not optional while using the python 3 strung format. The values can be a comma-separated list of values. Any data type is acceptable as a value.

  • In the below example, we printed the string that used the formatter.

Code:

print("Stud name is ABC".format(5))

Output:

Python 3 String Format 1

  • In this step, we are assigning the variable whose value is equal to the value of a string formatter.

Code:

open_string = "Python {}."
print(open_string.format("Python 3 string format"))

Output:

Python 3 String Format 2

  • We can also use multiple pairs of curly braces while using the formatters.

Code:

new_open_string = "Python {} {}."
print(new_open_string.format("Python 3 ","string format"))

Output:

Python 3 String Format 3

  • In the below example, we are using the index number and values to change the sequence in which they appear.

Code:

print("Python 3 {3}, {2}, and {1} {0}".format("Python", "3", "string", "format"))

Output:

Python 3 String Format 4

  • In the example below, we add indicate field size using curly braces.

Code:

print("ABC has {0:4} red {1:16}".format(5, "shirts"))

Output:

Python 3 String Format 5

Python 3 String Format Types

Different types are mentioned below:

  • %d – This format type is used for integer types.
  • %f – This format type is used for floating-point numbers.
  • %b – This format type is used for binary numbers.
  • %o – This format type is used for octal numbers.
  • %x – This format type is used for octal hexadecimal numbers.
  • %s – This format type is used for string.
  • %e – This floating-point is an exponent format.

The below example shows format types.

Code:

py = 'python'
print("Python %s 3 %s string"%('format',py))
print ('Python %s string.' %'format')
print ('Python %d string format.' %3)
print ('Pi value is: %5.4f' %(3.14))

Output:

Python 3 String Format Types

Python 3 String Format Method

The format for floating-point numbers is percent a.bf. If the complete number does not include this many digits, the minimal digits were present in the string, which was padded with white space. Closely related, bf denotes the number of digits to be presented following the decimal point.

The Format function was introduced in Python 3 to make it easier to handle sophisticated string formatting.

Float precision with placeholder method

The floating-point number uses the format of %a.f. The below example shows Float precision with the placeholder method.

Code:

print ('Pi value is: %4.5f' %(3.14))

Output:

Placeholder method

Formatting string by using the format method

Format method is introduced in python 3 to handle the formatting of strings. The below example shows the format method in python 3.

a) Format string using format method –

Code:

print('Python 3 string {}.'.format('format'))

Output:

Format method 1

b) Insert object using index-based position –

Code:

print('{2} {1} {0}'.format('Python', 'string', 'format'))

Output:

Format method 2

c) Insert object using assigned keywords –

Code:

print('P: {P}, Q: {Q}, R: {R}'.format(P = 10, Q = '20', R = 30))

Output:

Format method 3

Conclusion

String format (var1, var2, …) is the typical syntax of the format() function. Formatters. The value we want to put in the placeholders concatenated with the text supplied to the format method as parameters. The process of dynamically infusing things into a string and presenting it is known as string formatting.

Recommended Articles

This is a guide to Python 3 String Format. Here we discuss How to use the Python 3 string format along with the examples and codes. You may also look at the following articles to learn more –

  1. Python Add List
  2. Python Int to String
  3. Python Z Test
  4. Python Memory Error

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW