Functions

The print() Function

Displays the string value inside the parentheses on the screen. If there is no string inside the parentheses, it will display a blank line.

Sample code:

print('Hello Puchu!')

Output:
Hello Puchu!


The input() Function

It waits for the user to type some texts on the keyboard and press ENTER. It allows saving the user input into a variable.

Sample code:

myName = input()
print ('Hello ' + myName + '!')

Output:
Hello Puchu!

First line asks for an input that will be stored to the variable myName. The second line displays the word “Hello” plus the value provided for the variable myName.


The len() Function

It evaluates the integer value of the number of characters in a string.

Sample code:

myName="PuchuPuchuPuchu"
len(myName)

Output:
15

First line stores the string into the variable myName. The second line returns how many characters are there in the string stored to the variable.


The str(), int() and float() Functions

str()
This function evaluates to the string form of the value being passed.

int()
This function evaluates to the integer form of the value being passed. Integer means whole numbers only, which includes positive and negative.

float()
This function evaluates to the float form of the value being passed. Float means numbers with decimal points.

Sample code from interactive shell:

>>> ##STRING
>>> str('11.12')
'11.12'
>>> str('-11.12')
'-11.12'

>>> ##INTEGER
>>> int(11.12)
11
>>> int(-11.12)
-11

>>> ##FLOAT
>>> float('11.12')
11.12
>>> float(-11.12)
-11.12
>>>