Tutorial Python 5: Taking Input From User & Concatenation - Printable Version +- Red Security (https://redsecurity.info/cc) +-- Forum: Programming (https://redsecurity.info/cc/forumdisplay.php?fid=5) +--- Forum: Python (https://redsecurity.info/cc/forumdisplay.php?fid=9) +--- Thread: Tutorial Python 5: Taking Input From User & Concatenation (/showthread.php?tid=7885) |
Python 5: Taking Input From User & Concatenation - Covid-19 - 04-17-2022 Input Function: Now we are diving into something really interesting. We are making our program to wait until the user input something what we want. In Python input() function is used to take input from the user. Let’s code it for a better understanding of this function. Code: n = input(“Enter your name: ”) Firstly, let’s understand what this line of code does. We are telling our program to take input from the user and store it in a variable named ‘n’. But when user run this program it will show a prompt to user whatever you write between those quotations between the parentheses of the input function and waits until the user input something. But if you want to print what user stored inside the variable then use print function for it. Let’s code it.. Code: n = input(“What is your name? ”) Note: By default any value taken by input function is a String Wait, but what if we want to take input integer from the user like their age or some other numbers. So for that we have a function called ‘int()’ we use this function to convert the string to integer. Let’s do some code for better understanding… Code: n = int(input(“What is your age? ”) This code first takes input as a string and using int function convert it into integer. Program : Code: # Program to calculate your age at different years The last step in this program is something new. Let me demonstrate it for you. Here we use concatenation. It means adding two strings but wait what is that “str” ? Like we said before, concatenation means that adding two strings so before adding these we convert the integers into strings so that we can perform concatenation on them. str() converts the integer into strings. We can also use str() to convert floats into strings.
("Allah Hafiz" and "Good Bye" Till the next post ) "Keep Learning and Keep exploring" |