String Functions

Module: M3-R5: Python Programming

Chapter: Functions

🔹 Introduction to String Functions

Python provides a wide range of built-in string functions to manipulate, format, and check strings.

🔹 count()

Returns the number of occurrences of a substring.

text = 'hello world'
print(text.count('l'))

Output:

3
🔹 find & rfind()

Finds first/last index of a substring.

text = 'hello world'
print(text.find('o'))
print(text.rfind('o'))

Output:

4
7
🔹 capitalize()

Capitalizes the first character of the string.

text = 'hello world'
print(text.capitalize())

Output:

Hello world
🔹 title()

Capitalizes the first letter of each word.

text = 'hello world'
print(text.title())

Output:

Hello World
🔹 lower()

Converts string to lowercase.

text = 'HELLO WORLD'
print(text.lower())

Output:

hello world
🔹 upper()

Converts string to uppercase.

text = 'hello world'
print(text.upper())

Output:

HELLO WORLD
🔹 swapcase()

Swaps the case of each character.

text = 'Hello World'
print(text.swapcase())

Output:

hELLO wORLD
🔹 islower()

Checks if all characters are lowercase.

text = 'hello'
print(text.islower())

Output:

True
🔹 isupper()

Checks if all characters are uppercase.

text = 'HELLO'
print(text.isupper())

Output:

True
🔹 istitle()

Checks if string is in title case.

text = 'Hello World'
print(text.istitle())

Output:

True
🔹 replace()

Replaces a substring with another.

text = 'hello world'
print(text.replace('world','Python'))

Output:

hello Python
🔹 strip()

Removes leading and trailing spaces.

text = '  hello  '
print(text.strip())

Output:

hello
🔹 lstrip()

Removes leading spaces.

text = '  hello  '
print(text.lstrip())

Output:

hello  
🔹 rstrip()

Removes trailing spaces.

text = '  hello  '
print(text.rstrip())

Output:

  hello
🔹 split()

Splits string into a list.

text = 'hello world'
print(text.split())

Output:

['hello', 'world']
🔹 partition()

Splits string into 3 parts around a separator.

text = 'hello world'
print(text.partition(' '))

Output:

('hello', ' ', 'world')
🔹 join()

Joins iterable elements with string as separator.

words = ['hello','world']
print('-'.join(words))

Output:

hello-world
🔹 isspace()

Checks if string contains only whitespace.

text = '   '
print(text.isspace())

Output:

True
🔹 isalpha()

Checks if string contains only letters.

text = 'hello'
print(text.isalpha())

Output:

True
🔹 isdigit()

Checks if string contains only digits.

text = '123'
print(text.isdigit())

Output:

True
🔹 isalnum()

Checks if string is alphanumeric.

text = 'hello123'
print(text.isalnum())

Output:

True
🔹 startswith()

Checks if string starts with a substring.

text = 'hello world'
print(text.startswith('hello'))

Output:

True
🔹 endswith()

Checks if string ends with a substring.

text = 'hello world'
print(text.endswith('world'))

Output:

True
🔹 encode()

Encodes string to bytes.

text = 'hello world'
encoded = text.encode('utf-8')
print(encoded)

Output:

b'hello world'
🔹 decode()

Decodes bytes to string.

decoded = encoded.decode('utf-8')
print(decoded)

Output:

hello world
✅ Summary
  • String functions make text manipulation easy and readable.
  • Useful for searching, formatting, validating, and transforming strings.
  • Always test string functions with examples to understand their behavior.
Quick Links