Python 3D Academyবাংলা

String methods

🌍 In real life. At a photocopy shop, you hand over a page and ask for it "in bigger letters". You get back a new copy made bigger; your original page is exactly as it was. If you want the bigger one, you have to keep the copy.

String methods work like that shop: they hand you a new string and never touch the original.

The idea in one sentence: a method is a function that belongs to a value, called with a dot, and a string method always gives back a new string instead of changing the old one.

Its words:

  • method: a function attached to a value, like upper on a string
  • the dot: text.upper() means "the upper method of text"
  • gives back (returns): the new value a method hands you
  • chaining: calling a method on the result of another, text.strip().lower()

Step by step, for "hello".upper():

  1. Python takes the value "hello".
  2. It finds that string's upper method and runs it.
  3. Out comes a new string, "HELLO"; "hello" itself is unchanged.
text = "Hello World"
print(text.upper())
print(text.lower())
print(text)
The last line shows that text itself hasn't changed.

Why a new string? Strings can't be changed in place (Lesson 4.2), so a method can't edit the old one; it builds a new one. That's why you keep the result by putting it in a variable: text = text.upper(). Calling text.upper() on a line by itself makes the new string and then throws it away.

Methods for cleaning and changing text:

  • .strip() removes spaces from both ends
  • .replace(old, new) swaps every old for new
  • .title() starts each word with a capital letter
messy = "   rimi   "
print(messy.strip())
print("I like tea".replace("tea", "coffee"))
print("dhaka city".title())

Methods that answer questions about a string:

  • .startswith(x) and .endswith(x) give True or False
  • .find(x) gives the index where x first appears, or -1 if it isn't there
  • .count(x) counts how many times x appears
email = "rimi@mail.com"
print(email.endswith(".com"))
print(email.find("@"))
print("banana".count("a"))

.split() cuts a string into a list of words. (Lists get their own chapter, Chapter 8.) Add [-1] to pick the last word.

sentence = "I love Python"
print(sentence.split())
print(sentence.split()[-1])

You can chain methods: each one works on the result of the one before it.

print("  Hello  ".strip().lower())

Asking what a string holds. Methods whose names start with is answer True or False about the whole string: isdigit() (only digits), isalpha() (only letters), isalnum() (only letters and digits), isspace() (only spaces, tabs and new lines), isupper() and islower() (it has letters, and they are all capitals, or all small). An empty string gives False. Careful: a minus sign or a decimal point isn't a digit, so "-5".isdigit() is False. Bengali digits count as digits too: "২০২৬".isdigit() is True. Chapter 5 uses these checks on what a user types.

print("2026".isdigit(), "20.5".isdigit(), "-5".isdigit())
print("Dhaka".isalpha(), "Dhaka 1".isalpha())
print("".isdigit())
print("২০২৬".isdigit(), "A1".isupper())

Mistakes and edge cases.

  • Not keeping the result. name.upper() alone changes nothing; write name = name.upper().
  • Forgetting the brackets. print(text.upper) shows something like <built-in method upper of str object at 0x...>: the method itself, not its result.
  • A method name that doesn't exist. "hi".uppercase() gives AttributeError: 'str' object has no attribute 'uppercase'.
  • find() doesn't fail. When the text isn't there, .find() gives -1 instead of an error, and -1 is also a valid index, so check for it.
  • Capital letters matter. "Hello".startswith("h") is False.
  • replace() changes every match, not just the first one.

Quiz

  1. What does this print?

    print("abc".upper())
  2. What does this print?

    print("  hi  ".strip())
  3. What does this print?

    print("banana".replace("a", "o"))
  4. What does this print?

    print("python".find("t"))
  5. After s = "cat" and then s.upper(), what does s hold?

    • "cat"
    • "CAT"
    • Nothing: it's an error
  6. This should print RIMI, but it prints rimi. Which line needs fixing?

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

Coding tasks

  1. shout holds "hello". Make loud hold "HELLO!": the capital-letter version with "!" added.

  2. ip holds "192.168.0.1". Make defanged hold the IP with every . replaced by [.].

  3. raw holds " Python Rocks ". Print it without the spaces at the ends, in small letters:

    python rocks