Concepts / Data Types: Strings, Integers, and Floats

Data Types: Strings, Integers, and Floats

input() pauses your program and waits for the user to type something on their keyboard, then returns what they typed as a string.

  • Programming

From Script to Conversation

A hard-coded program always works with the values written into its script. An interactive program can ask the user for information, process the response, and then produce an answer. Python's input() function provides the pause between those stages: it waits for keyboard input, then gives the program what the user typed.

The Pause Before Enter

ProgramrunningPromptdisplayedKeyboard inputuser types a responseinput()waitingEnterpressedString valuereturned to programProgramcontinuescalls input()guidesresponse is enteredwaits forreleasesexecution resumes
What happens to program control before, during, and after the user types a response and presses Enter?

When Python reaches input(), it pauses the program and waits for the user to type on the keyboard. A prompt string can be displayed before the pause so the user knows what to enter. The program resumes after the user presses Enter. The value returned at that point is the text the user typed, represented as a string.

python
Output
What is your name? Alice
Hello, Alice

In this example, Python displays the prompt first. It then waits while the user types Alice and presses Enter. The returned string is stored in name, and the next print() statement uses that stored value.

Strings Before Numbers

A value returned by input() remains a string even when it looks like a number. For example, typing 15 gives input() the text "15". Python does not automatically make that text an integer merely because the characters are digits. To use the response in arithmetic, pass the returned string to int().

15typed by user"15"stringint()converts text15integer15 + 1arithmeticinput() returnspassed toproducescan be used in
How does the value returned by input() change when it passes through int(), and what data type does each step contain?
Worked example

Converting before calculating

A user enters 15. Store the response, convert it to an integer, and add 1.

  1. 1
    Capture

    input() waits for the user and returns the typed response as the string "15".

  2. 2
    Convert

    int() changes the returned string into the integer 15.

  3. 3
    Calculate

    The integer can now be used in the arithmetic expression 15 + 1.

  4. 4
    Display

    The result can be converted to a string with str() when it must be joined with surrounding text in a print() statement.

Result

The numeric result is 16.

python
Output
How old are you? 15
Next year you will be 16

The conversion can also happen in one line. In int(input("How old are you? ")), input() first waits and returns a string. The surrounding int() then converts that returned string before the value is assigned to age. This nested form is the standard concise way to obtain integer input.

Choosing the Right Conversion

  • Common mistake

    Assuming digits entered through input() are already integers

    What it looks like
    value = input("Enter a number: ")
    result = value + 1
    Why it fails

    input() returns the response as a string, even when the response consists of digits. The value has not been converted for integer arithmetic.

    Do this instead

    Use value = int(input("Enter a number: ")) before adding 1.

  • Common mistake

    Omitting the prompt string

    What it looks like
    response = input()
    Why it fails

    The program waits for keyboard input without telling the user what to enter.

    Do this instead

    Pass a guiding string such as input("Enter a number: ").

  • Common mistake

    Converting too late

    What it looks like
    value = input("Enter a number: ")
    text = "You entered " + value
    number = int(value)
    Why it fails

    The response is still a string until int(value) is applied. Any arithmetic must use the converted integer.

    Do this instead

    Convert as part of the assignment when the value is intended for arithmetic: value = int(input("Enter a number: ")).

  • Common mistake

    Treating the displayed form as proof of the data type

    What it looks like

    The user types 15, so the program is assumed to have received the integer 15.

    Why it fails

    The source rule concerns what input() returns: the returned value is the string "15".

    Do this instead

    Track the conversion explicitly: input() returns a string, and int() produces an integer.

StageExpressionValue formUse in this lesson
User response receivedinput("Enter a number: ")StringCaptures what the user typed
Response convertedint(input("Enter a number: "))IntegerAllows the captured value to be used in arithmetic
Floating-point conversionNot covered in the source materialNot specifiedThis lesson does not establish float behavior

The source material establishes the string result of input() and the integer conversion performed by int(). It does not provide a definition or conversion procedure for floats.

Try the Input Flow

EASY

Write a short Python program that asks the user to enter a number, converts the response to an integer, adds 10, and displays the result.

Hints
  • Put a prompt string inside input().
  • Wrap the input() call in int() before assigning the value.
  • Use a separate expression to add 10.

What do you think happens?

Before running the program, predict what type of value is assigned to response after the user types 8 and presses Enter.

  • The string "8"

  • The integer 8

  • The float 8.0

Reveal answer
Answer:

The string "8"

input() always returns a string. The response becomes an integer only when int() is applied.

Key Takeaways

  1. 1
    input() pauses execution, displays its prompt, waits for keyboard input, and resumes after the user presses Enter.
  2. 2
    The value returned by input() is always a string, including when the user types digits.
  3. 3
    Use int() to convert string input into an integer for arithmetic.
  4. 4
    The concise pattern int(input("prompt")) captures and converts numeric input in one line.
  5. 5
    This lesson establishes strings returned by input() and integers produced by int(); it does not develop float behavior.

At a glance

  • input() creates an interaction by pausing the program until the user types a response and presses Enter.
  • A prompt string tells the user what to enter before the program waits.
  • input() returns typed data as a string, so numeric-looking input is still text at first.
  • int(input("prompt")) converts the response to an integer immediately, allowing arithmetic.
  • The provided material focuses on strings and integers and does not specify how floats behave.

More concepts starting with D