Updated May 6, 2023
Introduction to Python Formatted String
The string formatting in python is done by the format() function, which formats the given value and places it inside the placeholder specified by curly braces, and the function returns the formatted string.
Syntax:
string.format(parameters)
The parameters contain one or more values that need to be formatted and inserted into the string. If there are more values that need to be formatted, every value must be entered, separated by commas. We can use any data type for the values. The number of specified placeholders should match the number of values passed as parameters in the format function. Otherwise, the compiler will throw an index error.
Examples of Python Formatted String
Given below are the examples of Python Formatted String:
Example #1
Let us see a simple code to demonstrate the string.format() method to get a basic idea about string formatting in python.
Code:
#using string.format() method
text = "my name is {}".format("jack")
print(text)
Output:
In the above example, we place the name jack in the placeholder defined by curly braces and assign the string to the value called text. Then, in the next line, the text is printed in the format specified by us.
Example #2
In the first example, we used one placeholder and passed one value to the placeholder. In this code, let us see how to pass multiple values using multiple placeholders.
Code:
#using string.format() function
text = "my name is {}, I am {} years old and I am a {} graduate".format("jack", 25, "ECE")
print (text)
Output:
In the above code, we have specified three placeholders and passed three values to the placeholders, which is evident in the output.
If the number of placeholders is greater than the number of values passed in the format function, then the compiler will throw an index error as follows:
Code:
#using string.format() function
text = "my name is {}, I am {} years old and I am a {} graduate".format("jack", 25)
print (text)
Output:
If the number of placeholders is lower than the number of values passed in the function, then it will not produce any error. Instead, it will create the output for the given number of placeholders.
Code:
text = "my name is {}, I am {} years old and I am a graduate".format("jack", 25, "ECE")
print (text)
Output:
In the above code, you can see that we have specified two placeholders in the string, but we have passed three values in the format function. The compiler does not show an error and still produces the output for the two placeholders, and two values passed. The third value passed is omitted by the compiler.
Different ways of using placeholder values:
Example #3
Code:
#placeholders with named indexes:
text1 = "My name is {name}, I'm {age} years old and I am an {degree} graduate".format(name = "Jack", age = 25, degree = "ECE")
#placeholders with numbered indexes:
text2 = "My name is {0}, I'm {1} years old and I am an {2} graduate".format("Jack", 25, "ECE")
#empty placeholders:
text3 = "My name is {}, I'm {} years old and I am an {} graduate".format("Jack", 25, "ECE")
print(text1)
print(text2)
print(text3)
Output:
In the above code, you can see that three different methods index the placeholder, but the output is still the same. In the first method, we have addressed the placeholder by assigning a name in the placeholder, and then we have assigned each name with a value in the format() function. In the next method, we have done indexing as 0, 1 and so on. Finally, in the third method, we haven’t specified any values. Python does the indexing automatically.
Types of Formatting:
We can format the given string by inserting a formatting type inside the placeholder and do various kinds of formatting such as aligning left or right, converting the result to binary, decimal or hexadecimal result and so on.
Let us see the various types of formatting with a few examples:
Aligning the Result:
- <: It aligns the result to the left.
- >: It aligns the result to the right.
- ^: It aligns the result to the center.
Example #4
Let us see a sample program which aligns with the result.
Code to demonstrate left alignment.
Code:
#India’s winning margin in last 5 matches
text1 = "india won by {:>10} runs"
text2 = "india won by {:>10} runs"
text3 = "india won by {:>10} runs"
text4 = "india won by {:>10} runs"
text5 = "india won by {:>10} runs"
print(text1.format(50))
print(text2.format(40))
print(text3.format(30))
print(text4.format(45))
print(text5.format(20))
Output:
In the above code, you can see all the values before the placeholder is aligned to the left by 10 spaces. Similarly, we can do left alignment and center alignment for the above code by changing the symbol.
Type Specifying:
- b: Converts the given value to binary format.
- d: Converts the given value to decimal format.
- x: Converts the given value to lower case hexadecimal format.
- X: Converts the given value to upper case hexadecimal format.
- o: Converts the given value to octal format.
- e: Converts the given value to scientific format. (lower case)
- E: Converts the given value to scientific format. (upper case)
Let us see a sample code to convert a given number to different formats.
Example #5
Conversion of a number to different formats.
Code:
#To get binary value
text = "The binary value of {0} is {0:b}"
print(text.format(10))
#To get octal value
text = "The octal value of {0} is {0:o}"
print(text.format(10))
#To get hexadecimal value
text = "The hexadecimal value of {0} is {0:x}"
print(text.format(10))
text = "The hexadecimal value of {0} is {0:X}"
print(text.format(10))
#To get scientific value
text = "The scientific value of {0} is {0:e}"
print(text.format(10))
text = "The scientific value of {0} is {0:E}"
print(text.format(10))
Output:
In the above code, we have converted the decimal value to different number formats using the format() method. By default, python considers 10 as decimal input.
Conclusion
In this article, we saw how to format strings in python a lot of examples. String formatting in python is important because it is used in organizing data. If the data is too large with a lot of values, the unorganized data will not look good. Here the format() function comes in handy.
Recommended Articles
We hope that this EDUCBA information on “Python Formatted String” was beneficial to you. You can view EDUCBA’s recommended articles for more information.