Computer Science

Python String Functions | len(), capitalize(), title(), lower(), upper()

Class 11 · Computer Science

Python String Functions (Part 1)

Python provides many built-in functions and methods to perform operations on strings efficiently. These functions help programmers calculate the length of a string, convert text into uppercase or lowercase, format headings, and modify text without writing lengthy programs.

In this article, we will study the following important string functions and methods:

  • len()
  • capitalize()
  • title()
  • lower()
  • upper()

Learning Objectives

  • Understand commonly used Python string functions.
  • Calculate the length of a string.
  • Convert strings into uppercase and lowercase.
  • Format strings for better presentation.
  • Apply string functions in real-life programming.

1. len() Function

The len() function returns the total number of characters present in a string. It counts every character including letters, digits, spaces, and special symbols.

Syntax

len(string)

Example 1

text="Python"
print(len(text))

Output

6

Example 2

text="Computer Science"
print(len(text))

Output

16

Explanation: The space between the two words is also counted.


Example 3

password="Abc@123"
print(len(password))

Output

7

Real-Life Applications of len()

  • Password length validation.
  • Checking mobile number length.
  • Validating PIN numbers.
  • Counting characters entered in forms.

2. capitalize() Method

The capitalize() method converts the first character of the string into uppercase and all remaining characters into lowercase.

Syntax

string.capitalize()

Example 1

text="python programming"
print(text.capitalize())

Output

Python programming

Example 2

text="PYTHON"
print(text.capitalize())

Output

Python

Example 3

text="computer SCIENCE"
print(text.capitalize())

Output

Computer science

Real-Life Applications of capitalize()

  • Formatting names.
  • Displaying headings.
  • Correcting user input.
  • Generating reports.

3. title() Method

The title() method converts the first letter of every word into uppercase and the remaining letters into lowercase.

Syntax

string.title()

Example 1

text="computer science"
print(text.title())

Output

Computer Science

Example 2

name="sunil mehta"
print(name.title())

Output

Sunil Mehta

Example 3

text="welcome to python programming"
print(text.title())

Output

Welcome To Python Programming

Difference between capitalize() and title()

capitalize() title()
Capitalizes only the first character of the entire string. Capitalizes the first character of every word.
"computer science" → "Computer science" "computer science" → "Computer Science"

4. lower() Method

The lower() method converts all uppercase letters into lowercase letters.

Syntax

string.lower()

Example 1

text="PYTHON"
print(text.lower())

Output

python

Example 2

text="Computer Science"
print(text.lower())

Output

computer science

Example 3

email="ADMIN@CODESTEPACADEMY.IN"
print(email.lower())

Output

admin@codestepacademy.in

Real-Life Applications of lower()

  • Email validation.
  • Username comparison.
  • Case-insensitive searching.
  • Database matching.

5. upper() Method

The upper() method converts all lowercase letters into uppercase letters.

Syntax

string.upper()

Example 1

text="python"
print(text.upper())

Output

PYTHON

Example 2

text="Computer Science"
print(text.upper())

Output

COMPUTER SCIENCE

Example 3

code="cs11"
print(code.upper())

Output

CS11

Explanation: Only alphabetic characters are converted into uppercase. Digits remain unchanged.


Real-Life Applications of upper()

  • Generating roll numbers.
  • Displaying headings.
  • Printing reports.
  • Formatting product codes.

Important Notes

  • All string methods return a new string.
  • The original string remains unchanged because strings are immutable.
  • Functions like lower(), upper(), and title() do not modify the existing string.

Example Demonstrating Immutability

text="Python"
text.upper()
print(text)

Output

Python

Correct Way

text="Python"
text=text.upper()
print(text)

Output

PYTHON

Quick Revision

  • len() → Returns the length of a string.
  • capitalize() → Capitalizes only the first character.
  • title() → Capitalizes the first character of every word.
  • lower() → Converts all letters to lowercase.
  • upper() → Converts all letters to uppercase.
  • String methods return a new string because strings are immutable.

6. count() Method

The count() method returns the number of times a specified character or substring appears in a string.

Syntax

string.count(substring)

Example 1

text="banana"
print(text.count("a"))

Output

3

Example 2

text="Computer Computer Science"
print(text.count("Computer"))

Output

2

Example 3

text="Mississippi"
print(text.count("ss"))

Output

2

Real-Life Applications of count()

  • Counting vowels in a string.
  • Counting spaces in a sentence.
  • Finding repeated words in a paragraph.
  • Counting occurrences of a keyword.

7. find() Method

The find() method searches for the first occurrence of a character or substring and returns its index position. If the substring is not found, it returns -1.

Syntax

string.find(substring)

Example 1

text="Computer Science"
print(text.find("Science"))

Output

9

Example 2

text="banana"
print(text.find("a"))

Output

1

Explanation: The first occurrence of a is at index 1.


Example 3

text="Python"
print(text.find("Java"))

Output

-1

Explanation: Since "Java" is not present, find() returns -1.


Real-Life Applications of find()

  • Searching words in a document.
  • Finding email domains.
  • Searching usernames.
  • Locating keywords in text.

8. index() Method

The index() method also searches for the first occurrence of a character or substring and returns its index position. However, if the substring is not found, it generates a ValueError.

Syntax

string.index(substring)

Example 1

text="Computer Science"
print(text.index("Science"))

Output

9

Example 2

text="banana"
print(text.index("n"))

Output

2

Example 3

text="Python"
print(text.index("Java"))

Output

ValueError

Difference between find() and index()

find() index()
Returns the index of the first occurrence. Returns the index of the first occurrence.
Returns -1 if the substring is not found. Raises ValueError if the substring is not found.
Safer when searching unknown data. Suitable when the substring is expected to exist.

Summary of Functions

Function / Method Purpose
len() Returns the length of the string.
capitalize() Capitalizes the first character.
title() Capitalizes the first letter of every word.
lower() Converts all letters to lowercase.
upper() Converts all letters to uppercase.
count() Counts the occurrences of a substring.
find() Returns the index of the first occurrence or -1.
index() Returns the index of the first occurrence or raises an error.

Common Programming Mistakes

1. Assuming find() Raises an Error

text="Python"
print(text.find("Java"))

Output

-1

The find() method never raises an error if the substring is absent.


2. Using index() Without Checking

text="Python"
print(text.index("Java"))

Result: ValueError


3. Forgetting that Strings are Case-Sensitive

text="Python"
print(text.find("python"))

Output

-1

Explanation: "Python" and "python" are different strings.


CBSE Important Programs

  1. Find the length of a string.
  2. Convert a string into uppercase.
  3. Convert a string into lowercase.
  4. Count the number of vowels.
  5. Count the occurrences of a particular character.
  6. Search for a word using find().
  7. Search for a word using index().
  8. Check whether a substring exists.
  9. Display a heading using title().
  10. Format names using capitalize().

Viva Questions

  1. What is the purpose of the len() function?
  2. What is the difference between capitalize() and title()?
  3. Differentiate between lower() and upper().
  4. What does the count() method return?
  5. What is the difference between find() and index()?
  6. Which method returns -1 when the substring is not found?
  7. Which method raises ValueError?
  8. Are string methods case-sensitive?
  9. Do these methods modify the original string?
  10. Give two practical applications of string functions.

Exam Tips

  • Remember that len() is a function, whereas the others are string methods.
  • find() returns -1 if the substring is absent.
  • index() raises a ValueError if the substring is absent.
  • Strings are case-sensitive.
  • String methods return a new string because strings are immutable.

Quick Revision

  • len() → Returns string length.
  • capitalize() → First letter uppercase.
  • title() → First letter of every word uppercase.
  • lower() → Converts all letters to lowercase.
  • upper() → Converts all letters to uppercase.
  • count() → Counts occurrences of a substring.
  • find() → Returns index or -1.
  • index() → Returns index or raises ValueError.