15  input function

We use input function to get user input from user.

15.1 input example 1

print("Please give me your Name")
name = input()
print("Hello",name)

15.2 input example 2

We can also give a string argument to input example.

name = input("Please give me your Name")
print("Hello",name)

15.3 input example 3

print("Please give me your Name")
name = input()
print("Hello",name)
print(f"Hello {name}, I am from XX")
print("Where are you from?")
country = input()
print(f"Hello {name} from {country}")

Download above code example

Input function always gives string input. If we need to change it to other types, we need to cast them.

15.4 input example 4: change data type

print("Please give your name")
name = input()
print("hello",name)
print("please enter your birth year")
birth_year_str = input()
birth_year = int(birth_year_str)
age = 2023-birth_year
print(f"hello {name}, you are {age} years old")

Download above code example input code - changing type from string

15.6 Video Tutorials