String Methods


Written by PIRATE KING

Methods

In this chapter, we'll explore some handy functions for working with strings. You've already seen the built-in len function, which is quite versatile and works with many types, not just strings. Later, I'll show you how to use it with other objects. But Python also has many functions made just for strings. These functions are called methods, a term you'll learn more about later. For now, just remember: in Python, everything is an object, and objects have methods that you can use by typing a dot . after them.

upper() & lower()

Suppose we have this code: name = "Pirate King". The method upper() converts all the letters in the string to uppercase, and lower() converts all the letters in the string to lowercase. Let's print and see:

We get PIRATE KING, all in uppercase, and pirate king, all in lowercase, respectively. These methods return a new string without changing the original one. So if you check name again after calling these methods, it will still be title case Pirate King. These methods are useful when you need to compare two strings without worrying about letter case or to display text consistently.

title()

title() converts the first letter of every word to uppercase and all other letters to lowercase. For example, applying it to "pirate king" yields "Pirate King".

It's handy for formatting names or titles automatically.

strip()

strip() removes extra spaces from the beginning and end of a string. It's useful for cleaning up user input or data from files. In this example, calling name.strip() removes the outer spaces around "Pirate King". To see the difference, let's also print the string lengths before and after calling strip().

len(name) returns 17, including the spaces, while len(name.strip()) returns 11—just the actual characters. Note that strip() only trims spaces at the edges, not the ones inside the string.

startswith() & endswith()

startswith() and endswith() check if a string begins or ends with a specific substring, respectively.

Since they are case-sensitive, name.startswith("pirate") with a lowercase p will return False because the variable name starts with an uppercase P.

in & not

Imagine you want to check if the word "rate" is somewhere inside another string, such as "Pirate King". You can do this simply with the in operator:

This prints True because the string "rate" is in the string "Pirate". Now, what if you want to check the opposite — is the string "Kingdom" not in the variable name? Use not in operators:

This prints True because "Kingdom" isn't in "Pirate King".

in and not in are great for quickly checking if a substring exists anywhere in the text.

find()

What about find()? It's also for searching, but instead of returning True or False, it gives the index where the substring starts — or -1 if it's not found. Let's look at some examples:

"Pirate" starts at index 0, "King" at 7, and "Kingdom" isn't there at all, so you get -1. You can use this method if you not only want to know if something is there, but also where it is.

replace()

Now, say you want to change any substring. For example, the "rate" of "Pirate" to something else. No problem. You can do this with the replace() method. replace() is a built-in method that returns a new string by substituting a specified substring with another. It takes two arguments: first, the old substring you want to search for and replace, and second, the new substring that will replace the old one. In cases where a function or method accepts multiple arguments, you separate them with commas. Let's change "rate" to "zza".

You get "Pizza King" as the new name. Notice how replace() created a new string with the changes — it did not modify the original variable, name, keeping the original text intact.

Quiz

1. What does the strip() method do in Python strings?

2. Which of the following string methods are used to check substrings in Python? (Select all that apply)

3. Given the string: text = "Hello, Python!", what will text.find("Python")

4. Given the string: sentence = "The quick brown fox jumps over the lazy dog.", write Python code to check and print if the word "fox" is in the sentence and the word "cat" is not.

Expected output:

True
True

5. Given the string: message = "This is a bad word, and another bad word." , censor the message by replacing the word bad with ***. Print the result.

Expected output:

This is a *** word, and another *** word.
© PIRATE KING 2025
GOLDSTONE STUDIO LLC
Privacy Policy