- Syntax
- Example 1: Removing Spaces from Both Ends
- Example 2: Removing Leading Spaces
- Example 3: Removing Trailing Spaces
- Example 4: Replacing a Word
- Example 5: Counting Characters
- Example 6: User Input
- Example 7: Replacing Multiple Occurrences
- Example 8: Counting a Substring
- Example 9: Combining Methods
- Example 10: Checking the Return Type
- Common Mistakes
- Best Practices
- Key Points to Remember
Python provides several string methods for removing unwanted spaces, searching within strings, and replacing text.
These methods are useful when processing user input, cleaning data, formatting text, and preparing strings for further processing.
In this lesson, we will study the following commonly used string methods:
strip()lstrip()rstrip()replace()count()
These methods return new strings (except count(), which returns an integer). The original string remains unchanged because strings are immutable.
Syntax #
string.method(argument)
or
string.method()
Components #
| Component | Description |
|---|---|
string |
The string object. |
. |
Dot operator used to access the method. |
method() |
The string method being called. |
argument |
Optional value required by some methods. |
Example 1: Removing Spaces from Both Ends #
main.py
text = " Python Programming "
print(text.strip())
Output #
Python Programming
Explanation #
strip()removes whitespace from both the beginning and the end of the string.- Spaces inside the string are not removed.
- The original string remains unchanged.
Example 2: Removing Leading Spaces #
main.py
text = " Python"
print(text.lstrip())
Output #
Python
Explanation #
lstrip()removes whitespace only from the left side.- The trailing characters remain unchanged.
- A new string is returned.
Example 3: Removing Trailing Spaces #
main.py
text = "Python "
print(text.rstrip())
Output #
Python
Explanation #
rstrip()removes whitespace only from the right side.- The beginning of the string is unchanged.
- The returned string contains no trailing spaces.
Example 4: Replacing a Word #
main.py
text = "I like Python."
print(text.replace("Python", "Java"))
Output #
I like Java.
Explanation #
replace()searches for"Python".- Every occurrence is replaced with
"Java". - The original string remains unchanged.
Example 5: Counting Characters #
main.py
text = "banana"
print(text.count("a"))
Output #
3
Explanation #
count()counts how many times"a"appears.- Three occurrences are found.
- The method returns an integer.
Example 6: User Input #
main.py
name = input("Enter your name: ")
print(name.strip())
Sample Input #
Alice
Output #
Alice
Explanation #
- The user enters a name with extra spaces.
strip()removes the spaces at both ends.- The cleaned string is displayed.
Example 7: Replacing Multiple Occurrences #
main.py
text = "cat bat cat bat"
print(text.replace("cat", "dog"))
Output #
dog bat dog bat
Explanation #
replace()replaces every occurrence of"cat".- Both instances become
"dog". - A new string is returned.
Example 8: Counting a Substring #
main.py
text = "Python Python Java Python"
print(text.count("Python"))
Output #
3
Explanation #
count()can count complete substrings."Python"appears three times.- The method returns the total count.
Example 9: Combining Methods #
main.py
text = " python programming "
print(text.strip().title())
Output #
Python Programming
Explanation #
strip()removes the surrounding spaces.title()capitalizes the first letter of each word.- Method chaining performs both operations in one statement.
Example 10: Checking the Return Type #
main.py
text = "banana"
result = text.count("a")
print(result)
print(type(result))
Output #
3
<class 'int'>
Explanation #
count()returns the number of matches.- The returned value is an integer.
- Its type is
int.
Common Mistakes #
1. Assuming strip() Removes All Spaces #
Incorrect
text = "Python Programming"
print(text.strip())
Incorrect Expectation #
PythonProgramming
Actual Output #
Python Programming
Reason
strip() removes spaces only from the beginning and end of the string.
2. Assuming replace() Changes the Original String #
Incorrect
text = "Python"
text.replace("Python", "Java")
print(text)
Output #
Python
Reason
replace() returns a new string.
It does not modify the original string.
Correct
text = text.replace("Python", "Java")
print(text)
3. Forgetting That count() Returns an Integer #
Incorrect
text = "banana"
result = text.count("a")
print(result.upper())
Output #
AttributeError: 'int' object has no attribute 'upper'
Reason
count() returns an integer, not a string.
4. Assuming lstrip() and rstrip() Remove Characters from Both Ends #
Incorrect
text = " Python "
print(text.lstrip())
Incorrect Expectation #
Python
Actual Output #
Python
Reason
lstrip() removes whitespace only from the left side.
Best Practices #
- Use
strip()to clean user input before processing it. - Use
replace()to substitute words or characters instead of manually rebuilding strings. - Use
count()to determine how many times a character or substring appears. - Remember that these methods return new values and do not modify the original string.
- Chain methods only when it improves readability.
Key Points to Remember #
strip()removes whitespace from both ends of a string.lstrip()removes whitespace from the left side only.rstrip()removes whitespace from the right side only.replace()returns a new string with the specified replacements.count()returns the number of occurrences of a character or substring.- Strings remain immutable after calling these methods.
count()returns a value of typeint, while the other methods returnstr.