GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.dremendo.com — print() Function and its Use in Python Program | Dremendo
?>
Announcements
No new announcements.
Python Programming Tutorial
Table of Contents
print() Function and its Use in Python ProgramIn this lesson, we will understand what print() function is and how it works in Python programming with the help of some examples. What is print() FunctionThe print() function in Python is used to output data to the standard output device, which is typically the console or terminal. It is one of the most commonly used functions for debugging and displaying information.
Note
In simple terms, a console is a screen or interface where you can see messages and type commands to interact with a computer.
Video Explanation
Click the button below to watch the explanation.
Please wait while the video is loading....
Let’s explore various examples to understand how the print() function works in Python.
Example 1
Python program to print the text Welcome to Dremendo on the screen.
Output
Welcome to Dremendo Welcome to Dremendo Here, we printed the text Welcome to Dremendo on the screen using the print() function. Whatever we place inside the parentheses (brackets) of the print() function will be displayed on the screen. You can use either single quotes or double quotes to print text using the print() function, as shown in the example above.
Example 2
Python program to print multiple items on the screen.
Output
Python 3.9 2024
Example 3
Python program to print Sunday and Monday on separate lines on the screen.
Output
Sunday Monday You can see that we used the print() function twice to print Sunday and Monday on separate lines on the screen.
Example 4
Python program to print Sunday and Monday on separate lines using a single print() function.
Output
Sunday Monday You can see that we used a single print() function to print Sunday and Monday on separate lines. We included \n (the escape sequence for a new line) right after Sunday, so that after printing Sunday, the line breaks and Monday is printed on the next line.
Note
The \n character is used to break a line and move the cursor to the beginning of the next line. You can use multiple \n characters to break additional lines, as shown in the example below.
Example 5
Output
Sunday Monday In the example above, the line breaks twice before Sunday is printed on the screen. After that, the line breaks twice again, and then Monday is printed. Use of sep Parameter in print() FunctionThe sep parameter in Python is commonly used in functions like print(), and it specifies the string that will be inserted between multiple arguments being printed. The sep parameter is used to format the output on the screen and it is available in Python from version 3.x or later.
Note
By default, the sep parameter is set to a space (' '), meaning if we print multiple items, they will be separated by a space.
Example
Python program to show the use of sep parameter in print() function.
Output
Sunday, Monday, Tuesday 18-06-2020 1 | 2 | 3 | 4 | 5 HelloWorld Use of end Parameter in print() FunctionThe end parameter in Python is commonly used in functions like print(), and it specifies the string that will be printed at the end of the output. This parameter helps control the formatting of the output, allowing us to print items on the same line or follow a specific format.
Note
By default, end is set to '\n', which means that after the printed output, the cursor moves to the next line.
Example: Default Behavior
Python program to show the default behaviour of end parameter in print() function.
Output
Hello World
Example: Changing the end Parameter
We can change the end parameter to any string we prefer. Here are some examples to illustrate its use:
Output
Sunday Monday Loading... Please wait. Hello I am Learning Python
Example: Use of sep and end parameters together in print() function
Python program to show the use of end and sep parameters together in print() function.
Output
You can see that we have used sep='' so thomas and wilson will be printed together without any space between them because the sep is blank. At the end of the line we have used end='@' as a result dremendo.com will be printed on the first line after @ character as shown in the output screen. Print Variable's Value using print() FunctionIn Python, we can easily print the value of a variable using the print() function. This is often used for debugging or displaying values during program execution. To print the value of a variable, simply pass the variable as an argument to the print() function.
Example
Python program to print string, integer and a float data type values on the screen.
Output
Alice 30 86.32 We can print multiple variables at once by separating them with commas.
Example
Python program to print multiple variables on the screen.
Output
Alice 30 New York We can use the sep parameter to customize the output.
Example
Python program to print multiple variables on the screen with a custom message.
Output
Addition of 10 and 20 = 30 a=10 b=20 c=30 Print Formatted String using % OperatorThe % operator in Python allows us to create formatted strings, meaning we can insert variables or expressions into specific placeholders within a string. This is called printf-style formatting, and it works similarly to the printf() function in C.
Video Explanation
Click the button below to watch the explanation.
Please wait while the video is loading....
Note
The % operator is an older way to format strings in Python, but it’s still useful to learn because:
Below are the list of symbols used with % operator for formatting outputs on the screen.
Example
Output
My name is Alice and I am 30 years old In the program above, we use format specifiers like %s (for string) and %d (for integer) as placeholders in the string. The values inside the parentheses (name, age) replace the placeholders in the same order. We can control how many decimal places a floating-point number shows using .nf — where n is the number of digits after the decimal point.
Example: Formatting a Float to 2 Decimal Places
Output
Value of Pi = 3.14 Here, .2f means to format the float with 2 decimal places. We can also format text alignment and width using % with width specifiers.
Example: Padding and Alignment
Output
|Bob | Bob | Bob| Here, %-10s means left-align the text and make it 10 characters wide, %s and name.center(20) means center-align the text and make it 20 characters wide, and %10s means right-align the text and make it 10 characters wide. Print Formatted String using format() FunctionThe format() function in Python allows us to create formatted strings, which means we can insert variables or expressions into specific placeholders within a string. This method provides a flexible and readable way to format strings.
Example
Output
My name is Alice and I am 30 years old In the program above, we use curly braces {} as empty spots in the string, and then we pass the values we want to insert into the format() function. These values will fill in the empty spots in the order we provide them in the format() function. We can control the order of the substitutions by using positional arguments in the placeholders:
Example: Using Positional Arguments
Output
Name: Alice, Age: 30 In this case, {1} refers to the second argument (name), and {0} refers to the first argument (age). We can control how many decimal places a floating-point number shows using format() function.
Example: Formatting a Float to 2 Decimal Places
Output
Value of Pi = 3.14 Here, {:.2f} means to format the float with 2 decimal places. Using format() function we can format numbers with commas as thousands separators:
Example: Formatting with Thousands Separator
Output
The number is: 1,000,000 We can also use format() function to specify padding and alignment of text and numbers. Left-align (<), center-align (^) and right-align (>).
Example: Padding and Alignment
Output
|Bob | Bob | Bob| Here, :<10 means left-align the text and make it 10 characters wide, :^20 means center-align the text and make it 20 characters wide, and :>10 means right-align the text and make it 10 characters wide. Print Formatted String using f-stringThe f-strings (formatted string literals) were introduced in Python 3.6 to make string formatting simpler, more readable, and faster. To use an f-string, prefix the string with f or F and place variables or expressions inside curly braces {}.
Example
Output
My name is Alice and I am 30 years old
Example: Formatting a Float to 2 Decimal Places
Output
Value of Pi = 3.14
Example: Formatting with Thousands Separator
Output
The number is: 1,000,000
Example: Padding and Alignment
Output
|Bob | Bob | Bob| Print the Data Type of Variables using type() FunctionIn Python, the type() function is used to determine the data type of a variable. We can use it to check the type of any object, such as integers, strings, lists, dictionaries, etc.
Video Explanation
Click the button below to watch the explanation.
Please wait while the video is loading....
Example: Use of type() function
Python program to show the use of type() function.
Output
Data type of 'name': <class 'str'> Data type of 'age': <class 'int'> Data type of 'height': <class 'float'> Data type of 'is_student': <class 'bool'> Data type of 'hobbies': <class 'list'> Data type of 'person': <class 'dict'> Escape Sequence Character in PythonEscape sequence characters are those that does not represent itself when used inside a character constant or string literal, but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly. In Python, all escape sequences characters start with a backslash (\) followed by one or more characters that determine the interpretation of the escape sequence. Common Escape SequencesHere are some of the most common escape sequences in Python:
Example
Output
Hello World Hello World \ "Hello" 'Hello' Helo World Helloage return
Note
On Windows terminal, the \b escape sequence moves the cursor back one space but does not delete the character. Any characters printed after the backspace overwrite previously written characters. Test Your KnowledgeAttempt the questions in the exercise to check whether you have understood the lesson clearly.
We care about your privacyDremendo uses cookies and similar technologies that are necessary for the site to work properly, understand how you interact with our website, and measure the effectiveness of our marketing. By clicking Accept All, you consent to the use of non-essential cookies and similar technologies for analytics, advertising, and ad measurement, as described in our Privacy Policy. Cookie PreferencesChoose which types of cookies Dremendo may use.
Necessary Cookies
Allow
Required for essential website functionality and security.
Analytics Cookies
Allow
Help us understand how visitors use Dremendo.
Marketing Cookies
Allow
Help us measure advertising performance and understand interactions with our advertisements.
Mentora
Question Asked: 0 / 20
Hi, I’m Mentora! I’m your personal AI tutor from Dremendo. Ask me anything, and I’ll help you understand it in a simple and easy way. | ||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |