Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = ' tea '
changed = text.strip().upper()
print(text)
print(changed)Top-level scope
' tea 'Object 2 · str'TEA'String methods are called on string objects. They return a result; transformation methods leave the original string unchanged.
(no output)
What do you expect this program to print?
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'Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = ' tea '
changed = text.strip().upper()
print(text)
print(changed)Top-level scope
' tea 'Object 2 · str'TEA'The example has finished. Compare the final output with the values and references above.
tea TEA
What do you expect this program to print?
text = ' tea '
changed = text.strip().upper()
print(text)
print(changed)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.
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 |
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!
lower(), replace(), split(), and join()Mastering string methods will help you with file handling, user input processing, and working with APIs or databases. Strings are everywhere in Python!
Connect what you noticed in the example with the idea below.
String methods are called on string objects. They return a result; transformation methods leave the original string unchanged.
Calling upper() changes the original string in place.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.