All TalkersCode Topics

Follow TalkersCode On Social Media

Print Format In Python

Last Updated : Aug 23, 2023

Print Format In Python

In this article we will show you the solution of print format in python, a key process in programming is printing, which enables us to show the user information or see the results of our code. The default tool for this in Python is the "print" function.

We can format and print text, variables, and expressions thanks to it. The print function's basic syntax is simple: we just type "print()" and the content we want to display inside of brackets.

We can pass variables and expressions as arguments or offer strings directly.

The print function in Python provides a number of formatting options to improve the result.

For instance, we can join strings together by using the "+" operator, or we can use the comma "," to divide things and have spaces automatically inserted between them.

To alter the layout, we may also utilize escape sequences like "\n" for new lines or "\t" for tabs. Now move to the concept of print format in python.

Step By Step Guide On Print Format In Python :-

name = "Alice"
age = 25
height = 1.65
# Simple printing
print("Hello, World!")
print("My name is", name)
# Concatenation with "+"
print("Name: " + name + ", Age: " + str(age) + ", Height: " + str(height))
# Comma-separated printing
print("Name:", name, "Age:", age, "Height:", height)
# Escape sequences
print("This is a new line.\nAnd this is a tab:\t\tTabbed text.")
# String formatting using placeholders
print("Name: %s, Age: %d, Height: %.2f" % (name, age, height))
# f-strings (formatted string literals)
print(f"Name: {name}, Age: {age}, Height: {height:.2f}")
  1. Name, age, and height are the three variables we have declared.
  2. Age is given the number 25, height is given the float value 1.65, and the variable name is given the string value "Alice".
  3. We use the print() function to display messages and values.
  4. Simply printing the string "Hello, World!" to the console is done in the first line of code, print("Hello, World!").
  5. The print() function is used to display a concatenated string in the following line.
  6. The age and height variables are converted to strings using the str() function, then various values are concatenated using the + operator.
  7. The name, age, and height data are all included in the final string.
  8. As an alternative, we can utilise the print() function's comma-separated syntax.
  9. This lets us display several numbers with commas separating them and automatically inserts spaces between them.
  10. The code also shows how escape sequences are used.
  11. The escape sequences n and t are used to denote a new line and a tab indentation, respectively, in the string print("This is a new line.\nAnd this is a tab:\t\tTabbed text.").
  12. These escape sequences are translated by the console when printed.
  13. The formatting of strings using placeholders is the next.
  14. The placeholders %s, %d, and %f stand in for string, integer, and float values, respectively, in the line print("Name:%s, Age:%d, Height:%.2f"% (name, age, and height)).
  15. After the % operator, the values are provided as a tuple (name, age, height).
  16. F-strings, also referred to as formatted string literals, are the last type.
  17. Curly braces are used in the line print(f"Name: {name}, Age: {age}, Height: {height:.2f}") to encapsulate variables within the string.
  18. The height variable is formatted to contain two decimal places thanks to the:.2f inside the curly braces.

Conclusion :-

As a result, we have successfully mastered the Python idea of print format.

We also discovered how adaptable Python's print() function is for showing information to users or visualising results.

It gives us a variety of formatting options for text, variables, and expressions.

I hope this article on print format in python helps you and the steps and mentioned above are easy to follow and implement.

Latest Tutorials