← Back to Blog

Methods — Your String’s Toolkit

The problem...

You can store text. You can access characters. You can slice.

But sometimes you need to do more.

Change the case. Remove extra spaces. Find out how long something is.

Doing that manually would take a lot of code.

The idea!

Strings in Python come with built-in tools. You don't need to build them yourself.

These tools are called methods.

Making it real

Think of it like this: your string is a document. Methods are the buttons in the toolbar.

Bold, uppercase, trim — already there. You just press the button.

In practice

You call a method by putting a dot after the string, then the method name.

name = "  Bull  "
print(name.strip())

Output → Bull

The dot means: "use this tool on this string."

The toolkit

len() — how long is the string?

name = "Bull"
print(len(name))

Output → 4

upper() — everything uppercase

name = "Bull"
print(name.upper())

Output → BULL

lower() — everything lowercase

name = "Bull"
print(name.lower())

Output → bull

strip() — remove spaces from both ends

name = "  Bull  "
print(name.strip())

Output → Bull

replace() — swap one thing for another

name = "Bull Horn"
print(name.replace("Horn", "Red"))

Output → Bull Red

Going further

Methods can be chained. One after another.

name = "  bull  "
print(name.strip().upper())

Output → BULL

Strip first, then uppercase. Python reads left to right.

What's really happening

Methods don't change the original string. They return a new one.

Your original value stays untouched unless you reassign it.

name = "bull"
name = name.upper()
print(name)

Output → BULL

Heads up!

  • len() is a function, not a method — it wraps the string, not dots after it
  • Methods don't modify the original string — they return a new value
  • strip() only removes spaces from the ends, not from the middle
  • Chaining works left to right — order matters

The mindset shift

Stop thinking: "I need to write code to transform text."

Start thinking: "My string already has tools. I just need to use them."

What you should understand now

  • Methods are built-in tools that come with every string
  • You call them with a dot: string.method()
  • len(), upper(), lower(), strip(), replace() are the basics
  • Methods return a new string — the original stays the same
  • Methods can be chained
[ login to bookmark ] // copied! 51 views · 1 min
// resources
Code Example strings_methods_1.py
← prev Take a Piece of the String next → Working With What’s Inside Strings
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.