Sometimes (more precisely, quite often), there are situations where you need to make a string, substituting in it some data obtained during the execution of the program (user input, data from files, etc.). Data substitution can be done by formatting strings. Formatting can be done using the % operator, and the format method. The format method is the most correct, but you can often come across a program code with string formatting in the form of the operator %. If you read through the following Python string format example, you will easily complete your own assignment.
The Python string functions example that you can find below was completed by an expert in programming. Why do you need this sample? Such tasks are often assigned to students – that’s why we have decided to help you with this problem. If you need help with assignments, you can check out our blog to find more samples.
Formatting Output in Python: Numbers, Strings
This guide investigates different ways of formatting the output of strings and numbers in Python.
Numbers
There are different situations when it is necessary to format numbers in output with a set number of decimal places, or thousands separator. So, the first example shows such output formatting. Also, numbers are aligned.
First of all, format() function is used to output numbers in the desired way. As its parameters, we specify a number that will be displayed and the way it will be showed in the console. For example, function on the first line has the parameters “a” and “>12,.2f.”
The first symbol “>” specifies alignment. Here we want the number to be right aligned, so this sign is used (for left alignment “<” is used; it is possible to place the number in the center, in this case the sign “^” is used). After this we specify the number of characters among which the number should be aligned. In this example, 12 characters are specified.
The next comma “,” is for the thousands separator in the integer part of the number. And the last part of formatting is about specifying the amount of characters after the decimal point. Here we have “.2f” which stands for two decimals after the point in the floating value. Also, there is a slightly different way of achieving the same output. It is possible by putting {} in the actual output message, and inside of the brackets put all the necessary formatting (here it will be put after a semicolon “:” – everything else is the same as in the example above).
Code:
Output:
Strings
As in the case with numbers, it is possible to format output with string using function format(). In the example below, we have an ordinary string with one placeholder ({}, called curly braces). When we add format() with some parameter to our string, these two lines are concatenated into one.
Code:
simple_string = “Holidays are coming {}”
print(simple_string.format(“very soon”))
Output:
Example of using multiple placeholders:
print(“{1} {0} are very {2}”.format(“days”, “These”, “cold”))
Output:
Here we see that it is possible to alter the order of outputted strings by specifying their index in curly braces. The first index is usually zero, so in the example above {1} is “These” and not “days”.
There are situations when it is necessary to display every character in string, even quotation marks. When displaying on the screen this message it is possible to use “!r” which will show everything without removing any characters from string like in the example below.
Code:
name = ‘Janine’
print(f”She was intoduced as {name!r}.”)
Output:
For another example datetime function is used. Python allows to enter numerical characters for date, and then month can be converted into a text-based representation.
Code:
Output:
And the last example is about outputting dictionaries. Again using format method we specify our dictionary books, and double “*” turn the values into correspondences like
“Lewis Carroll” = “Alice’s Adventures in Wonderland”.
During the process of writing this guide, the following resources were used:
https://www.python-course.eu/python3_formatted_output.php
https://www.digitalocean.com/community/tutorials/how-to-use-string-formatters-in-python-3