Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 5: Taking Input From User & Concatenation

0 Replies, 697 Views

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? ”)

print(n)


Idea 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? ”)

print(n)

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

birthYear = int(input("What is your birth Year? "))
ageAtYear = int(input("In which year you want to find your age? "))

yourAge = ageAtYear - birthYear

print("Your age in " + str(ageAtYear) + " will be " + str(yourAge))


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.
  • Exclamation  Task To Do:  Watch out the thread "Making a Text-Based-Adventure Game in Python Part.1 by Mad-Architect"


("Allah Hafiz" and "Good Bye" Till the next post )
"Keep Learning and Keep exploring"
Love me like you do Heart
(This post was last modified: 04-18-2022, 12:32 AM by Covid-19.)

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,227 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,607 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,645 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,825 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,591 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,823 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,772 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,802 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,804 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,799 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)