Python Character Sets & Tokens
print()
print (value/expr)
print(value, ...., sep=' ', end='\n', file=[Link], flush=False)
print(): Output displayed by the print() function will, by default, add an invisible \n newline
character at the end of the line to automatically move the print head to the next line.
Example:
>>> print () # Print a blank line
Reeta Sahoo & Gagan Sahoo
print(value/expression): This print() function prints the value of variable or expression.
Example:
>>> N1, N2 = 20, 30
>>> Sum = N1 + N2
>>> # Print a value
>>> print (Sum) # Prints: 50
Or
>>> # Print an expression
>>> print (N1 + N2) # Prints: 50
print(value, ...., sep=' ', end='\n', file=[Link], flush=False)
This print() function prints message, value, variable, expression, etc. You can print all at once.
Example:
print statement receives a string
message
print ("Hello friends!") # A message
Prints:
Hello friends!
A message A variable
print ("The sum is“ , Sum)
Prints: A comma
The sum is 50 separator Reeta Sahoo & Gagan Sahoo
Use sep & end separator:
The sep is a separator used in between the values. It defaults into a space character.
Example:
>>> print(10,20,30,40, sep=' * ') # Prints a space and separator *
Prints:
10 * 20 * 30 * 40
The end parameter is used to print the last printed character(s). It defaults into a new line.
Example:
>>> # A new end is assigned @
>>> print(10,20,30,40, sep=' * ', end='@')
Prints:
10 * 20 * 30 * 40@
Reeta Sahoo & Gagan Sahoo
The main purpose of this function is:
➢ Read input from the standard input (the keyboard, by default).
➢ By default you can input only the string values not numbers.
➢ This function accepts a string within its parentheses that will prompt the user for
input by displaying that string then wait to read a line of input.
The general format is:
input([prompt])
Here,
➢ The prompt is an optional parameter and where prompt is the string you wish to display on
the screen.
➢ The prompt is an expression that serves to prompt the user for input. This is almost always a
string literal which is enclosed within quotes (single or double) with parentheses.
➢ If the input result not converted, then the result of input() function displays as in string format.
Example:
>>> input("Enter any number: ")
Enter any number: 100
'100'
>>> input('Enter name: ')
Enter name: S. Anand
'S. Anand'
Notice that both input values '100' and 'S. Anand' are treated as string because it
displays within single quotation marks.
If you see carefully, you would probably notice the blank space inside the quotes at the
end of these prompts. We usually put a space at the end of a prompt so that the input
that the user types do not start right next to the prompt. Putting a space makes the
interaction easier to read and understand.
Reeta Sahoo & Gagan Sahoo
Assigning Value using input() Function
If you look at previous input() use, then the value we input is just flushed out. Now to retain the
value in memory we must assign into a variable.
The general format is: Try This:
Variable = input([prompt])
Example: In the shell window, click Ctrl+N to open a
>>> Num = input("Enter any number: ") window to writhe Python script and type
the following:
Enter any number: 100
>>> Name = input('Enter name: ') # input() function demonstration.
Enter name: S. Anand rollno = input("Enter your roll no. ")
>>> print ('Number is: ', Num) marks = input('Enter your total marks: ')
Number is: 100 print ('My roll no. is:', rollno)
print ('My mark is:', marks)
>>> print ('Name is: ', Name)
Name is: S. Anand Save the script as [Link] and execute to
see the result.
Reeta Sahoo & Gagan Sahoo
Converting input() Function Values
When a number like integer or float is assigned as a string or printed with print() statement, then
these numbers just display as in the form of strings. Such string inputs (numeric values) cannot be
perform any arithmetic operation. Let us see an example:
>>> Num1 = "20" Or Num1 = input ("Enter first no.: ")
>>> Num2 = '40' Or Num2 = input ("Enter second no.: ")
>>> print ('The sum of ', Num1, ' and ', Num2, ' is ', Num1+Num2)
The sum of 20 and 40 is 2040
Here, Num1 and Num2 are concatenated and produce a result 2040. So, it is necessary to
convert Num1 and Num2 into integer form.
To convert string into a number or integer form, we can use int() or float() functions.
int(). This function creates an integer equal to the string or number x.
Example: The int() function converts
string values into integer type
>>> Num1 = int ("20") Or Num1 = int (input ("Enter first no.: "))
>>> Num2 = int ('40') Or Num2 = int (input ("Enter second no.: "))
>>> print ('The sum of ', Num1, ' and ', Num2, ' is ', Num1+Num2)
Reeta Sahoo & Gagan Sahoo
The sum of 20 and 40 is 60
float(). This function creates a floating point number equal to the string or a number.
The float() function converts string
Example: values into floating-point number
>>> Val1 = float ("6.0224")
>>> Val2 = float (23)
>>> print ('The sum of ', Val1, ' and ', Val2, ' is ', Val1+Val2)
The sum of 6.0224 and 23.0 is 29.0224
Reeta Sahoo & Gagan Sahoo
Formatting String
Python strings are formatted in two ways:
- Using string modulo "%" operator.
- Using string format() function.
Using modulo (%) operator
In the modulo operator (%), a number of values may follow the % operator. These values are used up
by % placeholders in the format string. Also can be used with print() function.
The general format is: Argument 0
<string> % <values> Or print (<string> % <values>)
For example:
>>> City = 'Chennai‘
>>> ("My city is %s" % City) print("My city is %s" % City)
'My city is Chennai‘
>>> print("My city is %s" % City) Output String modulo
operator
My city is Chennai
My city is Chennai
For numeric formatting:
%<width>.<precision><type-char>
Let us see another example with multiple formatting values: Argument 0
Argument 1
Argument 2
print("My name %s, Age %4d and percentage %8.2f" % ('Prabhu', 14, 94.65))
Format string String modulo Tuple with
Output: operator values
My name Prabhu, Age 14 and percentage 94.65 2f
Here are some of the formatting characters # # # # . # #
Symbol Description
%7.2f
%c Character Note:
%s String ➢ The placeholders from left to right will proceed the tuple
values respectively.
%d Signed decimal integer ➢ %s is a string value placeholder.
%u Unsigned decimal integer ➢ %d is a signed decimal integer
➢ %8.2f is a format description for a float number. This
%i Integer number includes the decimal point and all the digits, i.e.
%f Floating number before and after the decimal point.
Using [Link]() function
There is a particular operation on strings called [Link]() that makes substitutions into places
enclosed in braces ( { } ).
The general format is:
[Link](p0, p1, ..., k0=v0, k1=v1, ...
Here, from the format:
➢ string is the object which will be formatted. We can use the print() function prompt as a string. The string format
objects are enclosed in braces { }.
➢ The format method is associated with string.
➢ Parameters are the values or objects which will be formatted and starting from default position {p0}.
➢ The positional parameter can also be placed with keyword arguments of the form name=value, i.e., k0=v0.
For example,
Argument 0
>>> Sname = input("Enter you name: ")
Index 0
Enter your name: Sunil Prabhu
>>> print ("My name is {}".format(Sname))
My name is Sunil Prabhu
When we leave curly braces empty without any
print (" My name is {}".format(Sname))
parameters, Python will replace the values passed
through the [Link]() method in order.
Reeta Sahoo & Gagan Sahoo
E.g.,
>>> print ("I am {}".format(14, Sname))
I am 14 Argument 0
>>> print ("I am {} {}".format(14, Sname))
Argument 1
I am 14 Sunil Prabhu
print ("I am {} {}".format(10, Sname))
Here, the first pair of curly braces is substituted with the string value of 14 and the second pair is
substituted with the string value of "Sunil Prabhu".
Using Keyword Parameters
The format string uses ordered parameters to substitute values/objects starting from 0. For example,
>>> "I love {a}, {b}, {c} and {d}" \
.format(a="Physics", b="Chemistry", c="Mathematics", d="Computer")
'I love Physics, Chemistry, Mathematics and Computer‘
>>> "I love {a}, {b}, {c} and {d}" \
.format(a="Physics", b="Chemistry", c="Mathematics") # keyword d=value is missing
Traceback (most recent call last):
File "<pyshell#16>", line 2, in <module>
.format(a="Physics", b="Chemistry", c="Mathematics")
Reeta Sahoo & Gagan Sahoo
KeyError: 'd'