Concepts / String Methods and Manipulation

String Methods and Manipulation

Strings are objects belonging to the str class and possess methods for checking, searching, and transforming text.

  • Programming

Strings as Objects

A Python string is more than a sequence of visible characters. It is an object belonging to the str class. Because it is a str object, it has methods for checking, searching, and transforming text. A method is an operation that you call through the string object, such as startswith(), find(), or join().

belongs toprovidesprovidesprovides"Python"string objectstrclassstartswith()checks a prefixfind()locates textjoin()combines strings
How is a string object connected to the str class and the methods available on it?

Checking a Prefix

The startswith() method checks whether a string begins with particular characters. It compares the beginning of the string with the prefix you provide and returns a result indicating whether the prefix matches.

python
Output
True
False

The first check succeeds because IJK is at the beginning of the string. The second check fails because TEC appears later, not at the beginning. startswith() is useful when a program needs to check a label, command, identifier, or other piece of text before deciding what to do next.

Locating Text with find

The find() method searches a string for a substring and returns the substring's numeric starting position in the original string. This lets you move from a text-search question to a position that a program can use.

containscontainsfind("TEC")returnsIJK TECoriginal stringIJKpositions 0–24starting positionspaceposition 3TECpositions 4–6
How does Python map the text found by find() to its numeric starting index in the original string?

Finding a Course Label

Use find() to locate TEC inside the string IJK TEC.

Start with the string: The text is IJK TEC.

Search for the substring: The expression label.find("TEC") asks the string to locate TEC.

Read the position: TEC begins after the three characters IJK and the space, so its numeric starting position is 4.

label.find("TEC") returns 4.

python
Output
4

Joining a Sequence

The join() method combines a sequence of strings into one string by placing a delimiter between the items. The delimiter is the string on which you call join(). For example, a comma-and-space delimiter can combine several words into a readable list.

between itemsbetween itemsfinal itemjoinsStringfirst item", "delimiterString, Methods,Pythoncombined stringMethodssecond itemPythonthird item
How does a separator move between the items in a sequence to produce one combined string?
python
Output
String, Methods, Python

Notice that join() is called on the delimiter, not on the sequence variable. The delimiter determines what appears between the items, while the sequence supplies the strings that are combined.

Method Results and Immutability

String methods return new values rather than modifying the original string. This is because strings are immutable. A method can inspect or transform text, but the original string remains unchanged unless you store the returned value in a variable or use it in another expression.

Discovering the str Toolkit

The three methods in this article are only part of the str class. Python's built-in help() function can show the available methods and their documentation. Use help(str) when you know that you need a string operation but do not yet know the method name.

python

Read the help output as documentation for the str class. It organizes the operations available to string objects, including methods for checking, searching, and transforming text. This makes help(str) a practical exploration tool rather than a method that changes a string.

A Method Pipeline

Practical string manipulation often combines several method results. A useful way to reason about such a task is to trace the operations in order: first inspect the text, then locate or produce a value, and finally use that value in the next step. Each method returns a value that can be assigned or used by later code.

course = "IJK TEC Academy" is_course = course.startswith("IJK") position = course.find("TEC") parts = ["String", "Methods", "Python"] topic_line = " | ".join(parts) print(is_course) print(position) print(topic_line)

inspectsearchcombineIJK TEC Academycoursestartswith("IJK")Truejoin(" | ")combined textfind("TEC")4String, Methods,Pythonsequence
What order do the string methods execute in, and how does each operation produce a value for the next part of the task?

Mistakes Beginners Make

  • Treating startswith() as a general search

    startswith() checks the beginning of the string, not an arbitrary location.

    Fix: Use startswith() for a prefix check and find() when you need to locate a substring.

  • Expecting find() to return the matching text

    find() locates the substring and returns its numeric starting position.

    Fix: Use the returned number as the position of the found text.

  • Calling join() on the sequence instead of the delimiter

    The delimiter is the string that calls join(); the sequence is supplied to it.

    Fix: Write ", ".join(parts).

  • Assuming a string method changes the original string

    String methods return new values and do not modify the original string because strings are immutable.

    Fix: Assign the returned value when you need to keep it.

Practice Tasks

EASY

Create a string containing a course name. Use startswith() to check its opening text, use find() to locate one word inside it, and use join() to combine three related topic names with a delimiter. Store each returned value and describe what each result represents.

Hints
  • Call startswith() on the course string.
  • Call find() with a substring that appears inside the course string.
  • Call join() on the delimiter and pass it a sequence of strings.
MEDIUM

Use help(str) to explore the complete set of methods available for string objects. Choose one method you have not used before and explain whether it checks, searches, or transforms text based on its documentation.

Hints
  • Run help(str) in a Python environment.
  • Look through the documented methods associated with the str class.
  • Connect the method's purpose to checking, searching, or transforming text.

Key Takeaways

  1. Strings are objects belonging to the str class, so they provide methods for working with text.
  2. startswith() checks whether a string begins with a specified prefix.
  3. find() locates a substring and returns its numeric starting position.
  4. join() combines a sequence of strings using a delimiter.
  5. String methods return new values rather than modifying the original string because strings are immutable.
  6. help(str) provides documentation for exploring the wider set of string methods.

Key Takeaways

  • A Python string is an object belonging to the str class.
  • Use startswith() for prefix checks, find() for locating text, and join() for combining strings with a delimiter.
  • String methods return values and do not modify the original string.
  • Use help(str) to discover and study additional string methods.