Text is a sequence of characters
A string is text in quotes. Single or double quotes, your choice, as long as they match:
city = "Kano"
country = 'Nigeria'
print(city + ", " + country) # Kano, Nigeria
print(len(city)) # 4
print(city.upper()) # KANO
print(city[0]) # Klen 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:
line = " 78, 81, 90 "
print(line.strip()) # "78, 81, 90"
print(line.strip().split(", ")) # ['78', '81', '90']
print("marks" in "exam marks") # Truef-strings, for building sentences
Joining with + gets ugly fast. Put an f before the quote and put values in curly braces:
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
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.
age = input("Your age: ")
print(type(age)) # <class 'str'>
print(age + 1)TypeError: can only concatenate str (not "int") to strThe 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:
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:
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:
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
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