• Home
  • 4.19 Common String Methods – Part 1

4.19 Common String Methods – Part 1

View Categories

4.19 Common String Methods – Part 1

4 min read

Python provides many built-in string methods that make it easy to manipulate and process text.

A string method is a function associated with a string object. Methods are called using dot notation (.) and usually return a new string without modifying the original string because strings are immutable.

In this lesson, we will study some of the most commonly used string methods:

  • upper()
  • lower()
  • capitalize()
  • title()
  • swapcase()

These methods are frequently used when processing user input, formatting text, and displaying information.


Syntax #

string.method()

Components #

Component Description
string The string object.
. Dot operator used to access a method.
method() Function that performs an operation on the string.

Example 1: Converting to Uppercase #

main.py

text = "Python Programming"

print(text.upper())

Output #

PYTHON PROGRAMMING

Explanation #

  • The upper() method converts all lowercase letters to uppercase.
  • The original string is not modified.
  • A new uppercase string is returned.

Example 2: Converting to Lowercase #

main.py

text = "Python Programming"

print(text.lower())

Output #

python programming

Explanation #

  • The lower() method converts all uppercase letters to lowercase.
  • The original string remains unchanged.
  • The returned string contains only lowercase letters.

Example 3: Capitalizing the First Letter #

main.py

text = "python programming"

print(text.capitalize())

Output #

Python programming

Explanation #

  • capitalize() converts only the first character to uppercase.
  • All remaining alphabetic characters become lowercase.
  • It is useful for formatting sentences.

Example 4: Converting to Title Case #

main.py

text = "python programming language"

print(text.title())

Output #

Python Programming Language

Explanation #

  • title() capitalizes the first letter of every word.
  • Each word begins with an uppercase letter.
  • This method is commonly used for titles and headings.

Example 5: Swapping Letter Cases #

main.py

text = "Python Programming"

print(text.swapcase())

Output #

pYTHON pROGRAMMING

Explanation #

  • swapcase() changes uppercase letters to lowercase.
  • Lowercase letters become uppercase.
  • Every alphabetic character has its case reversed.

Example 6: User Input #

main.py

name = input("Enter your name: ")

print(name.upper())

Sample Input #

Alice Johnson

Output #

ALICE JOHNSON

Explanation #

  • The user enters a name.
  • upper() converts every letter to uppercase.
  • The formatted string is displayed.

Example 7: Chaining Methods #

main.py

text = "python programming"

print(text.upper().lower())

Output #

python programming

Explanation #

  • upper() is executed first.
  • The returned string is immediately passed to lower().
  • Method chaining allows multiple operations in one statement.

Example 8: Checking That the Original String Does Not Change #

main.py

text = "Python"

print(text.upper())
print(text)

Output #

PYTHON
Python

Explanation #

  • upper() returns a new string.
  • The original string stored in text remains unchanged.
  • This demonstrates that strings are immutable.

Example 9: Storing the Returned String #

main.py

text = "python"

text = text.capitalize()

print(text)

Output #

Python

Explanation #

  • capitalize() returns a new string.
  • The returned string is assigned back to text.
  • Now the variable stores the updated value.

Example 10: Checking the Result Type #

main.py

text = "Python"

result = text.upper()

print(type(result))

Output #

<class 'str'>

Explanation #

  • String methods return another string.
  • The returned value is still of type str.
  • No new data type is created.

Common Mistakes #

1. Assuming upper() Changes the Original String #

Incorrect

text = "Python"

text.upper()

print(text)

Output #

Python

Reason

upper() returns a new string.
It does not modify the original string.

Correct

text = text.upper()

print(text)

2. Forgetting Parentheses #

Incorrect

text = "Python"

print(text.upper)

Output #

<built-in method upper of str object at ...>

Reason

Without (), Python refers to the method itself instead of calling it.

Correct

print(text.upper())

3. Expecting capitalize() to Capitalize Every Word #

Incorrect

text = "python programming"

print(text.capitalize())

Incorrect Expectation #

Python Programming

Actual Output #

Python programming

Reason

capitalize() affects only the first character of the entire string.

Correct

print(text.title())

4. Assuming swapcase() Reverses the String #

Incorrect

text = "Python"

print(text.swapcase())

Incorrect Expectation #

nohtyP

Actual Output #

pYTHON

Reason

swapcase() changes letter cases only.
It does not reverse the order of characters.


Best Practices #

  • Use upper() or lower() for case-insensitive comparisons.
  • Use title() when formatting names or headings.
  • Store the returned string if you want to keep the modified value.
  • Remember that string methods do not change the original string.
  • Use method chaining only when it improves readability.

Key Points to Remember #

  • String methods are called using dot notation.
  • upper() converts all letters to uppercase.
  • lower() converts all letters to lowercase.
  • capitalize() capitalizes only the first character.
  • title() capitalizes the first letter of every word.
  • swapcase() reverses the case of each letter.
  • These methods return new strings and do not modify the original string.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *