Python · Chapter 2.8 · Intermediate level

Talking to the User: input(), int() & String Superpowers

यूज़र से बात: input(), int() और String की ताक़तें

Real apps ask questions: your name, your age. input("Question? ") waits for the user to type and gives back the answer as text (str) — even if they type a number! So for maths we convert: age = int(input("Age? ")). Strings also have superpowers (methods): .upper(), .lower(), .title(), .strip(), len(text), text[0] (first letter) and text.replace("a", "b"). In this lab, type the answers in the 📥 Input box (one per line).

असली ऐप्स सवाल पूछते हैं: आपका नाम, आपकी उम्र। input("Question? ") यूज़र के टाइप करने का इंतज़ार करता है और जवाब टेक्स्ट (str) में देता है — चाहे नंबर लिखा हो! इसलिए गणित के लिए बदलते हैं: age = int(input("Age? "))। String के पास भी ताक़तें (methods) हैं: .upper(), .lower(), .title(), .strip(), len(text), text[0] (पहला अक्षर) और text.replace("a", "b")। इस लैब में जवाब 📥 Input box में लिखो (हर लाइन में एक)।

In the classroom you can run and edit this code in your browser, get hints and mark the chapter as done. क्लास में यह code ब्राउज़र में चलाकर बदल सकते हो।

Words and symbols in this chapter

input("Name? ")
Shows the question, waits, and returns what the user typed (always text).
सवाल दिखाता है, रुकता है, और यूज़र का लिखा लौटाता है (हमेशा टेक्स्ट)।
int(...)
Converts text like "15" into the number 15 so you can do maths.
"15" जैसे टेक्स्ट को नंबर 15 में बदलता है ताकि गणित हो सके।
name.title()
"aarav" → "Aarav" (capital first letter). Also .upper(), .lower().
"aarav" → "Aarav" (पहला अक्षर कैपिटल)। साथ में .upper(), .lower()।
len(name)
Counts the characters in a string.
String में कितने अक्षर हैं, गिनता है।
name[0]
First character (counting starts at 0!). name[-1] = last character.
पहला अक्षर (गिनती 0 से!)। name[-1] = आख़िरी अक्षर।

Ready example

name = input("Aapka naam? ")
age = int(input("Aapki umar? "))

print("Welcome,", name.title())
print("Naam mein", len(name), "akshar hain")
print("Pehla akshar:", name[0].upper())
print(f"Agle saal aap {age + 1} saal ke ho jaoge!")

Sample input typed into the Input box, one value per line:

aarav
15

What you will see: Change the values in the 📥 Input box (e.g. your name and age) and Run again.

📥 Input box की वैल्यू बदलो (जैसे अपना नाम और उम्र) और फिर Run करो।

Line by line

name = input("Aapka naam? ")
Takes the first line of the 📥 Input box.
📥 Input box की पहली लाइन लेता है।
age = int(input("Aapki umar? "))
Takes the next line and converts it to a number.
अगली लाइन लेकर उसे नंबर में बदलता है।
name.title() / len(name) / name[0].upper()
String methods: Capitalize, count letters, first letter in capital.
String methods: पहला अक्षर कैपिटल, अक्षर गिनना, पहला अक्षर बड़ा।
f"... {age + 1} ..."
Maths works because age is now an int.
गणित इसलिए हुआ क्योंकि age अब int है।

Common mistake

✗ Wrong

age = input("Age? ")
print(age + 1)

✓ Right

age = int(input("Age? "))
print(age + 1)

Text + number gives TypeError — convert the input with int() first.

टेक्स्ट + नंबर से TypeError आती है — पहले input को int() से बदलो।

Your task

Ask for a name and marks with input(), convert marks with int(), print the name in CAPITALS using .upper() and print marks + 5.

input() से नाम और marks पूछो, int() से marks बदलो, .upper() से नाम CAPITAL में print करो और marks + 5 print करो।

  1. Write marks = int(input("Marks? ")).
    marks = int(input("Marks? ")) लिखो।
  2. Write print(name.upper()).
    print(name.upper()) लिखो।
  3. Write print("Bonus ke saath:", marks + 5) and type 2 lines in the 📥 Input box (e.g. riya and 80).
    print("Bonus ke saath:", marks + 5) लिखो और 📥 Input box में 2 लाइनें लिखो (जैसे riya और 80)।
Starter code for the task
# 📥 Input box mein 2 lines likho: apna naam, aur ek number (marks)
name = input("Naam? ")
# TODO 1: marks = int(input("Marks? "))
# TODO 2: print karo naam CAPITAL letters mein (.upper())
# TODO 3: print karo marks + 5 (bonus)

Do this task in the classroom: your code is checked automatically and you get hints if something is missing.

Recap

Quick quiz

x = input() and the user types 5. What is type(x)?

x = input() और यूज़र 5 लिखता है। type(x) क्या होगा?

  1. int
  2. str
  3. float
  4. bool
Show the answer

Answer: B (str)

input() always gives text (str). Use int(x) to get a number.

input() हमेशा टेक्स्ट (str) देता है। नंबर के लिए int(x) लगाओ।

Practice problems for this chapter

Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.