Tutelage

Lessons › coding and computer science · taught by Leo

Strings and string methods explained

Learn what strings are and how methods like upper(), lower(), strip(), and find() work on text.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app

Strings are text in code

Strings are just text that code treats as data. If it is in quotes, the computer reads it as a string.

Think of a string like a necklace of characters, one after another.

The quotes are the signal that says, this is text, not a variable name.

String methods make changes

A string method is a built-in tool that works on a string, like uppercasing or searching.

Most methods do not change the original string. They give you a new result instead.

That is why you often save the answer in a new variable.

Predict: what is a string?

Which one is a string?

  1. 42
  2. "42"
  3. x + 1
Show answer

"42": Yes. The quotes make it text, so it is a string.

Use methods on a string

Let’s work one example. If word is cat, we can ask for the uppercase version.

We write word.upper(). The method belongs to the string on the left.

The result is CAT. But the original word is still cat unless we store the new value.

Quick check: original string

If word = "cat", what does word.upper() give you?

  1. "cat"
  2. "CAT"
  3. It changes word forever
Show answer

"CAT": Correct. upper() returns a new uppercase string.

Why methods return new text

Here is the big reason this works: strings are often kept unchanged so code stays predictable.

A method takes the string, does one job, and hands back a result.

That makes it easier to chain steps like cleanup, search, or formatting.

Common string methods

Three very common methods are upper, lower, and strip.

Upper and lower change letter case. Strip removes extra spaces from the ends.

These are tiny tools, but they show up everywhere in coding.

Predict: strip spaces

If text = " hi ", what does text.strip() return?

  1. "hi"
  2. " hi "
  3. "HI"
Show answer

"hi": Yes. strip() removes spaces at the ends.

Search inside a string

Strings can also be searched. A method like find looks for a smaller piece inside a bigger string.

If the piece is there, you get a position. If not, you get a special result like minus one in some languages.

That is why methods are useful: they help you inspect text without reading every character yourself.

Your turn: choose the method

You want to remove extra spaces from both ends of a string. Which method should you use?

  1. upper()
  2. strip()
  3. find()
Show answer

strip(): Right. strip() is the one that removes outside spaces.

Remember the string pattern

Here is the checklist to remember.

First, make sure it is a string. Then call the method on it.

Third, use the result the method returns. Save it if you want to keep it.

Recap: strings and methods

Strings are text inside quotes.

Methods are tools that act on strings.

Some common ones are upper, lower, strip, and find.

And the big habit is this: use the returned result, because the original string often stays the same.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app