Addaly is in open beta. Things will change, and AI answers can be wrong — check anything that matters.

Strings, and why input() always hands you text

Python, From Zero, For AI · lesson 3 of 10 · 6 min

Text is a sequence of characters

A string is text in quotes. Single or double quotes, your choice, as long as they match:

python
city = "Kano"
country = 'Nigeria'
print(city + ", " + country)   # Kano, Nigeria
print(len(city))               # 4
print(city.upper())            # KANO
print(city[0])                 # K

len counts characters. .upper() is a thing strings know how to do to themselves; the dot means "ask this value to do that". city[0] picks the first character, because Python counts positions from zero. That zero will matter again in the next lesson.

A few more that earn their keep:

python
line = "  78, 81, 90  "
print(line.strip())              # "78, 81, 90"
print(line.strip().split(", "))  # ['78', '81', '90']
print("marks" in "exam marks")   # True

f-strings, for building sentences

Joining with + gets ugly fast. Put an f before the quote and put values in curly braces:

python
name = "Amara"
marks = 81
print(f"{name} scored {marks} out of 100")
print(f"Half of that is {marks / 2}")

The braces are worked out as the line runs. Anything inside them can be arithmetic, not just a name.

input() asks the person running the program

python
name = input("Your name: ")
print(f"Hello, {name}")

Run it and the program pauses, shows the prompt, and waits for you to type and press Enter. Whatever you typed becomes the value of name.

The rule that causes the most beginner bugs

input() always gives you a string. Always. Even when the person typed digits.

python
age = input("Your age: ")
print(type(age))     # <class 'str'>
print(age + 1)
TypeError: can only concatenate str (not "int") to str

The person typed 20 and you got "20". Python cannot know that you wanted a number rather than a house number or a bus route. So you say it:

python
age = int(input("Your age: "))
print(f"Next year you will be {age + 1}")

Read that from the inside out: input(...) runs first and gives text, then int(...) turns that text into a number, then the name age is attached to the number.

The quieter version of the same bug

The crash above is the friendly case. Here is the unfriendly one:

python
budget = input("Monthly budget in pesos: ")   # you type 1200
if budget > "900":
    print("above")
else:
    print("below")

This prints below, and never complains. Both sides are text, so Python compared them the way a dictionary or a phone book does: character by character, left to right. "1" comes before "9", so "1200" sorts before "900" and the comparison is decided at the very first character. Length never enters into it.

Text comparison is not broken. It is answering a different question from the one you meant. The fix is to convert before you compare:

python
budget = int(input("Monthly budget in pesos: "))
if budget > 900:
    print("above")

A program that crashes tells you where it went wrong. A program that silently answers the wrong question does not. Convert at the edge, the moment data arrives, and the rest of your code can stop worrying.

Try this now

python
item = input("What did you buy? ")
price = float(input("Price? "))
qty = int(input("How many? "))
print(f"{qty} x {item} = {price * qty:.2f}")

The :.2f rounds to two decimal places. Then type abc when it asks for the price and read the error.

Before you move on

A program does `budget = input("Budget: ")` and then `if budget > "900":`. The user types 1200 and the program prints the else branch. What is the real explanation?

Pick the one you would defend. Nobody sees your answer.

No ads. No data sale. No public scores on people. Ever.

© 2026 Addaly