Lesson 17 of 70 – Python String Methods
24%

Python String Methods

Python provides many built-in string methods that make it easy to work with text. String methods can be used to change the case of text, search for characters, replace text, remove spaces, split strings, and perform many other operations.

Note: String methods are called using the dot . operator after a string variable or string value.
What are String Methods?

String methods are built-in functions provided by Python for working with strings.

name = "rahul"

print(name.upper())
Output:
RAHUL

Here, upper() is a string method.

upper() Method

The upper() method converts all alphabetic characters in a string to uppercase.

text = "hello python"

print(text.upper())
Output:
HELLO PYTHON
lower() Method

The lower() method converts all alphabetic characters to lowercase.

text = "HELLO PYTHON"

print(text.lower())
Output:
hello python
capitalize() Method

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

text = "hello python"

print(text.capitalize())
Output:
Hello python
title() Method

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

text = "python programming language"

print(text.title())
Output:
Python Programming Language
swapcase() Method

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

text = "Hello Python"

print(text.swapcase())
Output:
hELLO pYTHON
strip() Method

The strip() method removes spaces and specified whitespace from the beginning and end of a string.

text = "   Hello Python   "

print(text.strip())
Output:
Hello Python
lstrip() Method

The lstrip() method removes whitespace from the left side of a string.

text = "   Hello Python"

print(text.lstrip())
Output:
Hello Python
rstrip() Method

The rstrip() method removes whitespace from the right side of a string.

text = "Hello Python   "

print(text.rstrip())
Output:
Hello Python
replace() Method

The replace() method replaces one part of a string with another string.

text = "I like Java"

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

print(new_text)
Output:
I like Python
find() Method

The find() method returns the index of the first occurrence of the specified text.

text = "Hello Python"

print(text.find("Python"))
Output:
6

If the specified text is not found, find() returns -1.

print(text.find("Java"))
Output:
-1
index() Method

The index() method returns the index of the first occurrence of the specified text.

text = "Hello Python"

print(text.index("Python"))
Output:
6
Important: Unlike find(), index() raises a ValueError if the specified text is not found.
count() Method

The count() method returns the number of times a substring occurs in a string.

text = "banana"

print(text.count("a"))
Output:
3
startswith() Method

The startswith() method checks whether a string starts with a specified value.

text = "Python Programming"

print(text.startswith("Python"))
print(text.startswith("Java"))
Output:
True
False
endswith() Method

The endswith() method checks whether a string ends with a specified value.

filename = "student.pdf"

print(filename.endswith(".pdf"))
print(filename.endswith(".jpg"))
Output:
True
False
split() Method

The split() method divides a string into a list. By default, it splits the string at whitespace.

text = "Python is easy"

words = text.split()

print(words)
Output:
['Python', 'is', 'easy']
join() Method

The join() method combines elements of an iterable into a single string.

words = ["Python", "is", "easy"]

result = " ".join(words)

print(result)
Output:
Python is easy
isalpha() Method

The isalpha() method returns True if all characters in the string are alphabetic and the string is not empty.

print("Python".isalpha())
print("Python123".isalpha())
Output:
True
False
isdigit() Method

The isdigit() method returns True if all characters in the string are digits.

print("12345".isdigit())
print("123abc".isdigit())
Output:
True
False
isalnum() Method

The isalnum() method returns True if all characters are alphabetic or numeric and the string is not empty.

print("Python123".isalnum())
print("Python 123".isalnum())
Output:
True
False
isspace() Method

The isspace() method returns True if all characters in the string are whitespace characters and the string is not empty.

print("   ".isspace())
print("Hello".isspace())
Output:
True
False
islower() and isupper()

The islower() method checks whether the cased characters are lowercase, while isupper() checks whether they are uppercase.

text = "hello"

print(text.islower())
print(text.isupper())

text = "HELLO"

print(text.isupper())
Output:
True
False
True
String Method Example
text = "   python programming   "

print(text.strip())
print(text.upper())
print(text.title())
print(text.replace("python", "Java"))
print(text.count("p"))
Output:
python programming
PYTHON PROGRAMMING
Python Programming
Java programming
1
Common String Methods
Method Purpose
upper() Converts text to uppercase
lower() Converts text to lowercase
capitalize() Capitalizes the first character
title() Capitalizes each word
strip() Removes surrounding whitespace
replace() Replaces text
find() Finds the position of text
count() Counts occurrences
split() Splits a string into a list
join() Joins elements into a string
Key Points
  • String methods are used to perform operations on strings.
  • Methods are called using the dot . operator.
  • upper() converts text to uppercase.
  • lower() converts text to lowercase.
  • strip() removes surrounding whitespace.
  • replace() replaces specified text.
  • find() returns the position of specified text.
  • count() counts occurrences of text.
  • split() converts a string into a list.
  • join() combines iterable elements into a string.
  • isalpha(), isdigit(), and isalnum() are useful for checking string content.

🧠 Quick Quiz

Question: Which method converts a string into uppercase letters?