Home Course Roadmap
Lesson 17 · Strings

Python String Methods

Learn how to clean, search, modify, split, combine, validate, and format text using Python's powerful built-in string methods.

45–55 minutes
Beginner
Python Foundations
0%

By the end of this lesson, you will be able to:

  • Understand what string methods are.
  • Change the case of text.
  • Remove unwanted whitespace.
  • Search inside strings.
  • Replace portions of text.
  • Count occurrences of characters or words.
  • Split strings into lists.
  • Join list elements into strings.
  • Validate string content.
  • Chain multiple string methods together.
01

What Are String Methods?

A string method is a built-in operation that allows you to perform a specific task on a string.

Python provides many useful methods for working with text, including methods for changing case, searching, replacing, splitting, joining, and validating strings.

Basic syntax
string.method()
Example
name = "python"

print(name.upper())
Output
PYTHON
Important:

String methods usually return a new string. They do not modify the original string in place.

02

lower() & upper()

The lower() method converts letters to lowercase, while upper() converts letters to uppercase.

Example
text = "Hello Python"

print(text.lower())
print(text.upper())
Output
hello python
HELLO PYTHON

These methods are particularly useful when comparing text without worrying about uppercase or lowercase differences.

Case-insensitive comparison
answer = "YES"

if answer.lower() == "yes":
    print("Confirmed")
03

capitalize() & title()

capitalize() makes the first character uppercase and converts the remaining characters to lowercase.

title() converts the first character of each word to uppercase.

Example
text = "python programming language"

print(text.capitalize())
print(text.title())
Output
Python programming language
Python Programming Language
04

swapcase()

The swapcase() method changes uppercase letters to lowercase and lowercase letters to uppercase.

Example
text = "PyThOn"

print(text.swapcase())
Output
pYtHoN
05

strip()

The strip() method removes leading and trailing whitespace from a string.

Example
name = "   Adnan   "

clean_name = name.strip()

print(clean_name)
Output
Adnan
Very useful with user input

Users may accidentally type spaces before or after their input. strip() can clean those spaces.

06

lstrip() & rstrip()

lstrip() removes whitespace from the left side, while rstrip() removes whitespace from the right side.

Example
text = "   Python   "

print(text.lstrip())
print(text.rstrip())
Output
Python   
   Python
07

replace()

The replace() method replaces one piece of text with another.

Basic replacement
text = "I like Java"

new_text = text.replace("Java", "Python")

print(new_text)
Output
I like Python

You can also specify how many occurrences should be replaced.

Using count
text = "cat cat cat"

print(text.replace("cat", "dog", 2))
Output
dog dog cat
08

find()

The find() method searches for a substring and returns the index of its first occurrence.

Example
text = "I love Python"

position = text.find("Python")

print(position)
Output
7

If the substring cannot be found, find() returns -1.

Not found
text = "Python"

print(text.find("Java"))
Output
-1
09

index()

The index() method also searches for a substring and returns its position.

Example
text = "I love Python"

print(text.index("Python"))
Output
7
Important difference

If the text is not found, index() raises a ValueError, while find() returns -1.

10

count()

The count() method tells you how many times a substring appears in a string.

Example
text = "banana"

print(text.count("a"))
print(text.count("an"))
Output
3
2
11

startswith() & endswith()

These methods check whether a string starts or ends with a particular piece of text.

Example
filename = "report.pdf"

print(filename.startswith("report"))
print(filename.endswith(".pdf"))
Output
True
True

These methods are extremely useful when validating filenames, URLs, usernames, and other text patterns.

12

split()

The split() method divides a string into a list of smaller strings.

Splitting words
sentence = "Python is easy to learn"

words = sentence.split()

print(words)
Output
['Python', 'is', 'easy', 'to', 'learn']

By default, whitespace is used as the separator. You can also provide your own separator.

Custom separator
data = "Ali,25,Multan"

parts = data.split(",")

print(parts)
Output
['Ali', '25', 'Multan']
13

join()

The join() method does the opposite of split(): it combines strings from an iterable into one string.

Example
words = ["Python", "is", "powerful"]

sentence = " ".join(words)

print(sentence)
Output
Python is powerful

The string before .join() becomes the separator.

Different separators
items = ["Python", "HTML", "CSS"]

print(", ".join(items))
print(" | ".join(items))
print("-".join(items))
Output
Python, HTML, CSS
Python | HTML | CSS
Python-HTML-CSS

You've reached the end of this lesson.

Review the examples, open the Practice Lab, and mark this lesson complete when you're ready.

Open Practice Lab