15 input function
We use input function to get user input from user.
15.1 input example 1
print("Please give me your Name")
= input()
name print("Hello",name)
15.2 input example 2
We can also give a string argument to input example.
= input("Please give me your Name")
name print("Hello",name)
15.3 input example 3
print("Please give me your Name")
= input()
name print("Hello",name)
print(f"Hello {name}, I am from XX")
print("Where are you from?")
= input()
country print(f"Hello {name} from {country}")
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")
= input()
name print("hello",name)
print("please enter your birth year")
= input()
birth_year_str = int(birth_year_str)
birth_year = 2023-birth_year
age print(f"hello {name}, you are {age} years old")
Download above code example input code - changing type from string