Skip to content
Back to course

3. Working with text and user input

In this lesson you will discover how Python handles text — called strings — and how to make your programs interactive by asking users to type things in. By the end, you will write a small program that greets any person by name, just like a TeleBirr welcome screen greets each account holder.

In Python, any sequence of characters enclosed in quotation marks is called a string. A string can hold a name, a sentence, a phone number written as text, or even an empty space.

Here are some examples:

name = "Almaz"

city = 'Addis Ababa'

greeting = "Selam, how are you?"

phone = "0911-000-000"

The quotation marks tell Python: "everything between these marks is text, not code". You can use single quotes or double quotes — both create strings.

Notice how we stored each string in a variable. A variable is like a labelled box: you put a value inside and recall it later by the label. The = sign does not mean "equal" here — it means "store this value in this box".

Once you store text in a variable, you can print it:

name = "Abebe"

print(name)

This outputs:

Abebe

Notice — no quotation marks in the output! When you print a variable, Python shows its value, not the variable's name.

You can also join (concatenate) strings using the + operator:

first = "Abebe"

last = "Girma"

full = first + " " + last

print(full)

Output:

Abebe Girma

The " " in the middle adds a space between the two names. Without it you would get "AbebeGirma" — one squashed word. Concatenation is useful for building custom messages, like a receipt from a shop in Piassa that prints a customer's full name.

The + operator joins strings together, but it does NOT add numbers to strings directly. If you try print("Price: " + 150) Python will give you an error. You must convert the number to a string first using str(): print("Price: " + str(150)). You will learn more about this in the lesson on numbers and maths.

So far your programs have only displayed fixed text. Real programs are interactive — they ask questions and respond to what the user types. Python's input() function does exactly this:

name = input("What is your name? ")

print("Selam, " + name + "!")

When Python reaches the input() line it:

1. Shows the prompt message: What is your name?

2. Waits for the user to type something and press Enter.

3. Stores whatever the user typed in the variable name.

If Sara types Sara, the program then prints: Selam, Sara!

The message inside input("...") is called the prompt — it tells the user what to type. Always end your prompt with a space or a colon-space so the user's typed text does not appear right next to the question.

Let us combine everything into a small, useful program — a personalised greeter that could work at the start of any Ethiopian app:

# Personalised greeter

name = input("Enter your name: ")

city = input("Enter your city: ")

print("Welcome, " + name + "!")

print("We are glad you joined us from " + city + ".")

print("Your TeleBirr account is ready.")

If the user types Almaz and Hawassa, the output is:

Welcome, Almaz!

We are glad you joined us from Hawassa.

Your TeleBirr account is ready.

By changing a few lines you could adapt this for a pharmacy in Jimma, a coffee export company, or a school registration form. That is the power of combining strings and input() — your program responds to real people.

Joining strings with + works, but it can get messy with many pieces. Python offers a cleaner tool called an f-string (formatted string). You put f before the opening quote and then place variable names directly inside {} braces:

name = "Sara"

city = "Dire Dawa"

print(f"Welcome, {name}! You are logged in from {city}.")

Output:

Welcome, Sara! You are logged in from Dire Dawa.

The f at the start tells Python: "look for {} placeholders and fill them with the variable values". f-strings are the modern, preferred way to build text in Python. They are easier to read than long + chains — especially when a message has three or more pieces.

Scenario

Dawit is writing a program that asks a user for their phone number and then prints it. He writes: phone = input("Enter your Ethio Telecom number: ") print("Your number is: " + phone) When he runs it and types 0912345678, what does Python print?

Lesson recap: • A string is any text enclosed in quotation marks — single or double. • Store strings in variables using =, then print the variable name to display the value. • Join strings together with + (concatenation) — remember to add spaces between pieces. • input("prompt") asks the user to type something and returns it as a string. • f-strings (f"Hello {name}") are the cleanest way to embed variable values inside text. • Useful methods: .strip() removes edge spaces, .upper() / .lower() change case. • input() always returns a string — convert to a number with int() or float() if you need maths.

Check your understanding

1/7 · 80 XP

Which of the following is a valid Python string?