Skip to content

Working with Text (Strings)

Programming Python Difficulty 1/5

Beginning of the lesson

Working with Text (Strings)

Read, cut, clean, and join text like a pro

Big Idea: In Python, any piece of text is called a string. Names, messages, passwords, and web pages are all strings. Once you can pick letters out of text, tidy it up, and glue pieces together, you can build almost anything that talks to people.
By the end you will be able to:
  • pick a single letter with indexing and a piece with slicing,
  • tidy text with .upper(), .lower(), and .strip(),
  • join text with concatenation (+),
  • build clean sentences with f-strings.

1. What is a string?

A string is text wrapped in quotes. You can use single quotes '...' or double quotes "..." — both work the same. The quotes tell Python "this is text, not a number or a command".

name = "Python"
print(name)
print(len(name))

Output:

Python
6

len(name) counts the characters in the string. "Python" has 6 letters, so it prints 6. Spaces count too!

2. Indexing: picking one letter

Every letter in a string has a position number called an index. Here is the key surprise: counting starts at 0, not 1. You can also count backwards from the end with negative numbers, where -1 is the last letter.

index from the front index from the back P 0 -6 Y 1 -5 T 2 -4 H 3 -3 O 4 -2 N 5 -1
Each letter of "PYTHON" has a position. Blue counts from the front (starting at 0); orange counts from the back (starting at -1).

Put the index in square brackets right after the string:

word = "PYTHON"
print(word[0])   # first letter
print(word[2])   # third letter
print(word[-1])  # last letter

Output:

P
T
N

So word[0] is P, word[2] is T, and word[-1] is the last letter N.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers