Strings, print and its different types, How to use print as Debugger
Published by
sanya sanya
Introduction:
Python uses strings, which are collections of characters, to represent text-based data. To work with strings, Python comes with a number of built-in functions and techniques. The print() method is one of Python's most utilized functions. We'll talk about many print statements in this blog, including the print(f""),.format(), simple print, and% technique. With clear explanations and examples, we will also go through how to use print statements as a debugger.
Print statement types:
print(f""):
Python added the print(f"") function, which is a formatted string literal or f-string. It helps the formatting of the output by enabling the incorporation of expressions inside string literals. To place expressions inside the string, use curly brackets.
Example:
https://images.codingblocks.com/data-science/print%28f%29.png
.format():
String formatting in Python is done using the.format() function. It enables us to use placeholders to add values to strings.
Example:
https://images.codingblocks.com/data-science/format%28%29.png
Simple print:
The simplest way to print strings in Python is to use the simple print statement. It only requires one argument, a string, and prints it to the console.
Example:
https://images.codingblocks.com/data-science/simple_print.png
The % technique
It is a more traditional method for formatting strings in Python. Placeholders %s for strings,%d for integers, and %f for floating-point values are used in this programme.
Example:
https://images.codingblocks.com/data-science/perc_technique.png
Here, in this example %d is used for the integer and %s is used for the string.
Python's floating-point numeric formatting is done via the %f function. As a parameter, it accepts a floating-point number and formats it to a fixed-point notation with a predetermined number of decimal points.
Syntax:
https://images.codingblocks.com/data-science/perc_technique_syntax.png
Here, value is the floating-point integer we want to format, and n is the number of decimal places we want to display.
Examples:
Displaying a floating-point value with two places for the decimal:
https://images.codingblocks.com/data-science/exm_1.png
The %f technique was used in the example above to format the value of pi to two decimal places.
Displaying a floating-point value with four places for the decimal:
https://images.codingblocks.com/data-science/exm_2.png
The %f technique was used in the example above to format the value of pi to four decimal places.
Multiple floating-point numbers with various decimal places displayed:
https://images.codingblocks.com/data-science/exm_3.png
In the example above, we formatted the values of a and b to different decimal places using the %f technique. Additionally, we have passed multiple values to the print statement using brackets.
Use of variables in the format string
https://images.codingblocks.com/data-science/exm_4.png
In the example above, the price and tax values were stored in variables, and the values were formatted to two decimal places using the %f technique. For a sensible way to show the values, we have additionally used a format string.
Debugging using print statements
Debugging is the process of identifying and correcting code errors. Use of print statements is one of the most efficient debugging techniques. The values of variables can be printed using print statements, allowing us to observe how they change while the programme executes.
Example:
https://images.codingblocks.com/data-science/debugging.png
Conclusion
In conclusion, Python's print statements are a crucial tool for handling strings. Depending on our needs, we can utilize a variety of print statements, including print(f""),.format(), basic print, and % technique. To find bugs in our code, we may also utilize print statements as a debugger.
Even though each of the methods we discussed has pros and cons, f-strings and the.format() method are more recent and popular ways to format strings in Python. They support a broader variety of formatting possibilities and are easier to read. F-strings are also quicker and shorter than the.format() approach.
The % method, a more traditional approach to string formatting, has some drawbacks. It can be difficult to comprehend and maintain in complex code and does not offer a wide variety of formatting options.
In conclusion, even though print statements are an effective tool in Python, using the proper sort of print statement can improve our code's readability, maintainability, and effectiveness.
Library
WEB DEVELOPMENT
FAANG QUESTIONS
On this page
Introduction:
Print statement types:
print(f""):
.format():
Simple print:
The % technique
Use of variables in the format string
Debugging using print statements
Conclusion