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

Python, From Zero, For AI

From your first line of code to your first API call.

Lesson 6 of 896 min

Strings, and why input() always hands you text

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.

input() handed you text: what each comparison then answers'9' against '10''100' against '99'Compared as textCompared as numbersTrue'9' > '10'True'100' < '99'False9 > 10False100 < 99Text comparison is not broken. It compares character by character, left to right, the way a phone bookdoes, so '1' sorts before '9' whatever the numbers mean. The program never complains, which is whatmakes it worse than a crash. Convert at the edge: age = int(input(...)).
input() handed you text: what eachcomparison then answers'9' against '10''100' against '99'Compared as textTrue'9' > '10'True'100' < '99'Compared as numbersFalse9 > 10False100 < 99Text comparison is not broken. It compares characterby character, left to right, the way a phone bookdoes, so '1' sorts before '9' whatever the numbersmean. The program never complains, which is whatmakes it worse than a crash. Convert at the edge:age = int(input(...)).

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.

The one thing to keep

input() always returns text, so convert it the moment it arrives or you will compare numbers alphabetically.

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