Page 450 - CITS - Computer Software Application -TT
P. 450
COMPUTER SOFTWARE APPLICATION - CITS
flush : (Optional) A Boolean, specifying if the output is flushed (True) or buffered (False). Default: False
Returns: It returns output to the screen.
Example: Python Print Output
• Python3
# Python program to demonstrate
# print() method
print(“NSTI”)
# code for disabling the softspace feature
print(‘N’, ‘S’, ‘T’, ‘I’)’)
Output
NSTI
N S T I
In the above example, we can see that in the case of the 2nd print statement there is a space between every
letter and the print statement always add a new line character at the end of the string. This is because after every
character the sep parameter is printed and at the end of the string the end parameter is printed. Let’s try to change
this sep and end parameter.
Example: Python Print output with custom sep and end parameter
• Python3
# Python program to demonstrate
# print() method
print(“NSTI”, end = “@”)
# code for disabling the softspace feature
print(‘N’, ‘S’, ‘T’, ‘I’, sep=”#”)
Output
NSTI@N#S#T#I
Formatting Output
Formatting output in Python can be done in many ways. Let’s discuss them below
Using formatted string literals
We can use formatted string literals, by starting a string with f or F before opening quotation marks or triple
quotation marks. In this string, we can write Python expressions between { and } that can refer to a variable or
any literal value.
Example: Python String formatting using F string
• Python3
# Declaring a variable
name = “Nsti”
# Output
print(f’Hello {name}! How are you?’)
Output:
Hello Nsti! How are you?
437
CITS : IT&ITES - Computer Software Application - Lesson 120 - 137 CITS : IT&ITES - Computer Software Application - Lesson 120 - 137