Simple Arithmetic, Input and Output in Python
Text Output
The built-in print()
function of Python is used to display output on the command line terminal. There are alternative uses of this function as shown in :
print ("Learning Python is interesting.")
Output: Learning Python is interesting.
print ("Learning Python is interesting.","Right?")
Output: Learning Python is interesting. Right?
print ("Learning","Python","is","interesting.","Right?")
Output: Learning Python is interesting. Right?
String literals are expressed between a pair of double quotation marks.
Any number of space characters placed in a string literal is part of that string.
Quotation marks can be placed inside a string literal by using a backslash ("\") character right before the quotation mark, as in \"
, without any space in between.
print("This line contains a pair of \"quotation marks\"")
Output: This line contains a pair of "quotation marks"
String literals can be assigned to variables to be used wherever required
course_code = "Cmpe150"
course_name = "\"Introduction to Computing\""
print("The code of this course is: ", course_code)
print("The full name of this course is: ", course_code, course_name)
course_full_name = course_code + course_name
print("Alternatively, the full name of this course is: ", course_full_name)
course_full_name = course_code + " " + course_name
print("The corrected full name of this course is: ", course_full_name)
Similar to using quotation marks in a string literal escaped by the \
character, the backslash itself can be used inside the string by escaping it again with another backslash:
print("Here is a single backslash \\ used in a string literal")
Output: Here is a single backslash \ used in a string literal
Another escape character is the \n
newline character that we can use to split the string literal into multiple lines:
print("Here is a newline \n used in a string literal")
Output:
Here is a newline
used in a string literal
It is also possible to use \'
as the single quote, and \t
as a tab character inside the strings.
print("This output does not have a newline at the end", end='')
print(" so that the next call to the print() can continue outputting on the same line.")
Output:
This output does not have a newline at the end so that the next call to the print() can continue outputting on the same line
You can also change the separator character for the print()
function:
print("this","is","an","example","to","changing","the","default","separator","space","character", sep='_*_')
Output:
this_*_is_*_an_*_example_*_to_*_changing_*_the_*_default_*_separator_*_space_*_character
print ("10 times 2 is",10*2)
print ("2 to the power 3 is",2**3)
my_age = 20
city = "Istanbul"
print ("Age is",my_age, ". I live in ", city)
You can experiment with the given source code using the following example: