input()
Let’s show the use of the input()
function simply by taking the name from the user.
name = input("Enter your name: ")
print(name)
Output:
Enter your name: Baransel
Baransel
As seen above, we received a name information from the user. We printed the information we received with the print()
function, which we processed in our previous blog post.
For example, let’s take two numbers from the user and add them
number1 = input("Enter the first number: ")
number2 = input("Enter the second number: ")
sum = number1 + number2
print("Total: ", sum)
Output:
Enter the first number: 52
Enter the second number: 45
Total: 5245
As you can see, we got the result of 1225, it actually gave the numbers 12 and 25 written side by side, not the sum of these two numbers.
With the
input()
function, we can only get String (text) data types from the user.
In other words, it took numbers in String type, so how do we import data of Integer (integer) type?
number1 = input("Enter the first number: ")
number2 = input("Enter the second number: ")
sum = int(number1) + int(number2)
print("Total: ", sum)
format()
method:
name = input("Your name: ")
age = input("Your age: ")
print("Hello {}, your age {} you are still young".format(name, age))
Your name: Baransel
Your age: 22
Hello Baransel, your age 22 you are still young
We did the same job in a much easier and simpler way. The format()
method took the parameters it took, instead of the curly braces, and here’s what we need to pay attention to; The order of the parameters taken by the curly braces and the format()
method is the same.