Updated February 21, 2023
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.
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.
- Using the percent operator to format.
- The format string method is used to format the data.
- F-strings are string literals that are used to format data.
- 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:
- 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:
- 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:
- 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:
- 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 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 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:
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:
b) Insert object using index-based position –
Code:
print('{2} {1} {0}'.format('Python', 'string', 'format'))
Output:
c) Insert object using assigned keywords –
Code:
print('P: {P}, Q: {Q}, R: {R}'.format(P = 10, Q = '20', R = 30))
Output:
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 –