Loop Control Statements in C Introduction to HTML How to use the Github API The image tag, anchor tag and the button tag Ordered and Unordered Lists in HTML The division tag HTML Forms Tables in HTML Introduction to C Programming Introduction to Python Varibles and Datatypes in Python Operators in Python Typecasting in Python Input and Output in Python If Else in Python Loops in Python Break, Continue and Pass in Python Python practice section 1 Lists in Python Tuple in Python

Input and Output in Python

In any language we have two important things, one is to take input or we can say take data from the user and another is to show output to the user.

Showing output

In python we use 'print()' function to show output to user. Consider the following code for better understanding:

# show string
print("In this way we can show string")
# now we will learn to print empty line
print() 
# "\n" -> this mean print new line
print("\n") 
# show value of variable
s = "string stored in variable"
print(s)
a = 15
print(a)
    
Output:
In this way we can show string



string stored in variable
15
As we have seen above, the print function prints value of the variable or data given in its parenthesis and then prints an empty line. If we do not want to print an empty line at end of the print statement then we can do this in the following way:

print("text 1")
# text 2 will print in new line as print statement insert new line in end
print("text 2")

# In this way we can change new line to anything else
print("text 3",end=" ")
print("text 4")

# One more example
print("Rapid",end="@")
print("coders")
        
Output:
text 1
text 2
text 3 text 4
Rapid@coders

Taking input from user

In python we use 'input()' function to take input from user. Consider the following code for better understanding:

a = input()

print(a)
    
Input:
    Rapid Coders
Output:
    Rapid Coders
Now we will see how to show a message to the user to enter data:

a = input("Enter Your Name : ")

print(a)
    
Input:
    Enter Your Name : Rapid Coders
Output:
    Rapid Coders
Note: In Python, input is always taken in form of a string. Even if you enter an integer or a floating point number, the value will be stored as a string. Later we can typecast this string into float or int. Consider the below code:

a = input("Enter Your name : ")

print(type(a))
print(a)

print()

b = input("Enter Number = ")
print(type(b))
print(b)

print()
# lets typecast b
b = int(b)
print(type(b))
print(b)
    
Input:
    Enter Your name : Rapid Coders
    Enter Number = 123
Output:
    <class 'str'>
    Rapid Coders
    
    <class 'str'>
    123
    
    <class 'int'>
    123