In this article, we will learn about
- A basic thumb rule to remember
- Print function with Single and Double Quotes
- Printing Variables using print() function
- Performing mathematics inside print() function
- Printing using f-String
- Use of Backslash preceded characters inside the print() function
- String Formatting using {} Curly Brackets and format()
- String Formatting using C Programming Language % style format specifiers
- String concatenation, end = ” ” concept inside print() function
A basic thumb rule to remember
The print function in Python takes arguments separated by commas. An argument can be simply a text placed inside a single/double quote or it can be a name of a variable.
For example:
print(“I am demo text”, x,’print me’,”why not”,y)
print(Arg1, Arg2, Arg3, Arg4,……,ArgN)
Print function with Single and Double Quotes
You can print any text if placed inside single and double quotes inside the print() function.
Code: When using single and Double quotes
print('Demo print: Enclosed inside single quotes')
print("Demo print: Enclosed inside double quotes")
Output:
Demo print: Enclosed inside single quotes Demo print: Enclosed inside double quotes
Printing Variables using print() function
Now, we will discuss how to print variables without a format specifier. As you we discussed in thumb rule, you can simply use any number of argument inside a print function. To print a variable without format specifier just print the name of the variable.
Code: Printing Variable without Format String
x = 5
print("Let's print Variable x",x, 'See it is working')
Output:
Let's print Variable x 5 See it is working
Explanation:
In the above code we used three-argument:
- The first argument is a string: text placed inside Double Quotes
- The second argument is a variable name
- The third argument is a string: text placed inside Single Quotes
Performing mathematics inside print() function
We can also perform mathematic operations inside the print() function. The mathematics expression will evaluate to a numeric data value, thus should not be placed inside quotes.
Code: Performing maths operation inside print() function
x = 5
y = 10
z = 10
print("Let's perform addition", x + y + z)
print("Let's perform Subtraction", x - y, z - x)
print("Let's perform Multiplication", x * y * z)
print("Let's perform Division", z / x)
Output:
Let's perform addition 25 Let's perform Subtraction -5 5 Let's perform Multiplication 500 Let's perform Division 2.0
Printing using f-String
Let’s take an example to understand the need for the format string. If you want to display the value of a variable inside a string placed between a single or double-quotes. Please go through the following code and output.
Code: Trying to print
x = 105
y = "Python 3 Program"
print("x y")
print(f"{x} {y}")
Output:
x y 105 Python 3 Program
Explanation:
In the above code, you can see that when we are trying to print the value of x and y at Line number 3, the only variable name is printed not value. Here f-string comes to use, when a string with single and double quotes is started with f character, we can print the actual value of any variable enclosed within brackets {} often referred to as Curly brackets.
So, the f-string is a way to format strings. The code below will help you to understand this concept better.
Code: Assigning format string to a variable and use it to print
x = 1209
y = "The value of x is"
z = f"{y} {x}"
print(f"{x}")
print(f"{y}")
print(z)
print(f"x value is : {x}")
print(f"The value y is : {y}")
Output:
1209 The value of x is The value of x is 1209 x value is : 1209 The value y is : The value of x is
Use of Backslash preceded characters inside the print() function
We have already studied about Backslash character in the post String Literals in Python, Backslash helps to hide the meaning of single and double quotes but it also helps to print some important characters in the Terminal output listed in the table given below:
| Escape Sequence | Meaning |
|---|---|
| \\ | Backslash (\) |
| \' | Single-quote (') |
| \" | Double-quote (") |
| \a | ASCII bell (BEL) |
| \b | ASCII backspace (BS) |
| \f | ASCII formfeed (FF) |
| \n | ASCII linefeed (LF) |
| \N{name} | Character named name in the Unicode database (Unicode only) |
| \r | Carriage return (CR) |
| \t | Horizontal tab (TAB) |
| \uxxxx | Character with 16-bit hex value xxxx |
| \Uxxxxxxxx | Character with 32-bit hex value xxxxxxxx |
| \v | ASCII vertical tab (VT) |
| \000 | Character with octal value 000 |
| \xhh | Character with hex value hh |
Code: Printing Backslash Characters
print("Print New Line character \n")
print("Print Single quote: \'")
print("Print Double quote: \"")
print("Print HorizontalTab: \t")
print("Print Vertical Tab \v")
Output:
Print New Line character Print Single quote: ' Print Double quote: " Print HorizontalTab: Print Vertical Tab
String Formatting using {} Curly Brackets and format()
-
format() function to allow multiple substitutions and value formatting
We discussed f-String in the above section which helps to substitute the value of a variable inside the string. There is a function called format() which facilitates to allow multiple substitutions inside a string through positional formatting.
Positional formatting refers to the usage of any number of curly braces inside a string and with the help of format function, we will substitute the value based on the position of curly braces.
Usage can be understood from the given example:
"I am a {} programmer with {} years of experience".format("Python", 10)
The below syntax refers to the above usage:
string.format()
- Where string refers to a string variable or string placed using single or double quotes
- Dot format .format() is the syntax to call the function
Input type: format() takes input as any Data type.
Return type: string.format() function returns a string with substituted values used for curly brackets positions.
a = "I am a {} programmer with {} years of experience".format("Python", 10)
print(a)
print("Learn {} {} by practise".format("Python", "Programming"))
format_string = "{} {} {} {}"
print(format_string.format(1, 3, 4, 9))
print(format_string.format("I am", "a", "Python", "Programmer"))
print(format_string.format("I have", 10, "years of experience", "In Python"))
print(format_string)
Output:
I am a Python programmer with 10 years of experience
Learn Python Programming by practise
1 3 4 9
I am a Python Programmer
I have 10 years of experience In Python
{} {} {} {}
Advanced Printing using C Programming Language % style format specifiers
Just now we have studied the concept of format string using curly braces {} and format function that helps to print different data types using the print function. You can achieve the same with the % format string.
If you have studied C Programming language, you must be aware of format specifiers mentioned below
| % Format Specifier | Type |
|---|---|
| %d | integer |
| %f | float |
| %s | string |
| %x | hexadecimal |
| %o | octal |
The following syntax is used for % format strings
print("%d %s %d....%f" %(var1, var2, var3....varN)
The following code will help you to grasp the concept of the format string
Code: Printing using % format string
a = 5
b = "I am "
c = "a"
d = "Python Programmer"
print("%s %s %s with %d years of experience" % (b, c, d, a))
Output:
I am a Python Programmer with 5 years of experience
String concatenation, end = ” ” concept inside print() function
In this section, we will discuss
-
String concatenation inside the print function
-
Multiplying a string by a number to print multiple strings
- end = ‘ ‘ in python
-
String concatenation inside the print function
Code: String Concatenation Inside Print Function
var_1 = "I " var_2 = "am " var_3 = "a " var_4 = "Python " var_5 = "Program " print(var_1 + var_2 + var_3 + var_4 + var_5)
Output:
I am a Python Program
-
Multiplying a string by a number to print multiple strings
Code: Multiplying a string inside Print Function
var_1 = "I "
print(var_1 * 10)
print("Python " * 3)
Output:
I I I I I I I I I I Python Python Python
-
end = ” ” in python
end = ” ” or end = ‘ ‘ is used when the programmer wants to stay on the same line, instead of going to the next line which is the default behavior of print() function after printing anything.
Code: Display the use of end = ” ” or end = ‘ ‘
var_1 = "I "
print(var_1 * 10, end=" ")
print("Python " * 3)
Output:
I I I I I I I I I I Python Python Python