Python String Methods

Let’s look at string methods

  1. Notice

    String methods are called on string objects. They return a result; transformation methods leave the original string unchanged.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = ' tea '
changed = text.strip().upper()
print(text)
print(changed)
Live state

Top-level scope

text→Object 1
changed→Object 2
Object 1 · str' tea 'Object 2 · str'TEA'
This step

String methods are called on string objects. They return a result; transformation methods leave the original string unchanged.

Picture string methods

String methods are called on string objects. They return a result; transformation methods leave the original string unchanged.

Image brief

Alt text
A diagram for Python String Methods. Instructions: text = ' tea ' changed = text.strip().upper() print(text) print(changed). Displayed result: tea TEA. The final example bindings are text --> object 1 (str); changed --> object 2 (str). object 1: ' tea '; object 2: 'TEA'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String Methods". Teaching focus: String methods are called on string objects. They return a result; transformation methods leave the original string unchanged. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = ' tea ' changed = text.strip().upper() print(text) print(changed) | v DISPLAYED RESULT tea TEA FINAL BINDINGS text --> object 1 (str) changed --> object 2 (str) object 1: ' tea ' object 2: 'TEA' Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String Methods. Instructions: text = ' tea ' changed = text.strip().upper() print(text) print(changed). Displayed result: tea TEA. The final example bindings are text --> object 1 (str); changed --> object 2 (str). object 1: ' tea '; object 2: 'TEA'

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = ' tea '
changed = text.strip().upper()
print(text)
print(changed)
Live state

Top-level scope

text→Object 1
changed→Object 2
Object 1 · str' tea 'Object 2 · str'TEA'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
 tea 
TEA

What do you expect this program to print?

What does this example display?

text = ' tea '
changed = text.strip().upper()
print(text)
print(changed)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

tea TEA

Follow the printed values in order. The output is: tea TEA

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

String methods are called on string objects. They return a result; transformation methods leave the original string unchanged.

String methods are called on string objects. They return a result; transformation methods leave the original string unchanged.

More examples and reference

Python String Methods

In Python, strings are one of the most commonly used data types. To help you work with text easily, Python gives you many built-in string methods.

These methods let you change, check, or find things in strings without writing a lot of extra code.

List of Python String Methods

Below is a complete list of all string methods in Python, explained simply so even beginners can understand.

Method Description
capitalize()Converts the first character to upper case
casefold()Converts string into lower case (more aggressive than lower())
center()Returns a centered string with padding
count()Counts how many times a value appears in the string
encode()Encodes the string using a specified encoding (like UTF-8)
endswith()Checks if the string ends with the specified value
expandtabs()Sets the tab size (replaces tabs with spaces)
find()Returns the first position of a value, or -1 if not found
format()Formats strings using placeholders
format_map()Formats strings using a dictionary map
index()Like find(), but raises an error if not found
isalnum()Returns True if all characters are letters or numbers
isalpha()Returns True if all characters are letters
isascii()Returns True if all characters are ASCII
isdecimal()Returns True if all characters are decimals
isdigit()Returns True if all characters are digits
isidentifier()Returns True if the string is a valid Python identifier
islower()Returns True if all characters are lowercase
isnumeric()Returns True if all characters are numeric
isprintable()Returns True if all characters can be printed
isspace()Returns True if the string only contains whitespace
istitle()Returns True if each word starts with a capital letter
isupper()Returns True if all characters are uppercase
join()Joins items in an iterable into one string
ljust()Returns a left-justified version of the string
lower()Converts all characters to lowercase
lstrip()Removes leading whitespace or characters
maketrans()Creates a translation map for use with translate()
partition()Splits the string into 3 parts: before, match, and after
replace()Replaces part of the string with another string
rfind()Finds the last occurrence of a value
rindex()Like rfind(), but raises an error if not found
rjust()Returns a right-justified version of the string
rpartition()Splits the string from the right into 3 parts
rsplit()Splits the string from the right
rstrip()Removes trailing whitespace or characters
split()Splits the string by a separator
splitlines()Splits the string at line breaks
startswith()Checks if the string starts with the specified value
strip()Removes whitespace or characters from both ends
swapcase()Swaps lowercase to uppercase and vice versa
title()Converts the first character of each word to uppercase
translate()Returns a translated version of the string
upper()Converts all characters to uppercase
zfill()Fills the string with zeros from the left

How to Use This List

Click on any method name in our full course (coming soon) to see syntax, examples, and beginner exercises. Practice using them to build your confidence working with strings!

Next Steps

  • Start with common ones like lower(), replace(), split(), and join()
  • Try combining multiple methods for more advanced text processing
  • Use interactive Python shell or an online compiler to test each method

Mastering string methods will help you with file handling, user input processing, and working with APIs or databases. Strings are everywhere in Python!

What to remember

Connect what you noticed in the example with the idea below.

Remember

String methods are called on string objects. They return a result; transformation methods leave the original string unchanged.

Watch out for this idea

Calling upper() changes the original string in place.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Course overview →