🧰 Python String Functions — Mastering Built-in String Methods

Introduction 🌟

Python provides many powerful built-in string functions (also called string methods) that allow you to manipulate, format, and analyze text easily. These functions help you clean data, search words, format output, and more.

Note

💡 String functions **do not modify the original string** — they return a new one because strings are immutable in Python.

1. Changing Case 🔤

1. upper()

Converts the entire string to uppercase.

upper.py

text = "hello python"
print(text.upper())   # HELLO PYTHON

2. lower()

lower.py

print("HELLO".lower())   # hello

3. title()

Capitalizes the first letter of every word.

title.py

print("hello world".title())   # Hello World

4. capitalize()

Capitalizes only the first letter of the string.

capitalize.py

print("python basics".capitalize())   # Python basics

5. swapcase()

Swaps uppercase ↔ lowercase.

swapcase.py

print("PyThOn".swapcase())   # pYtHoN

2. Searching Within Strings 🔍

1. find()

Returns the index of the first occurrence of a substring.

find.py

text = "python programming"
print(text.find("gram"))   # 10
print(text.find("Java"))   # -1

2. index()

Like find(), but raises an error if substring is not found.

index.py

text = "python"
print(text.index("t"))   # 2
# print(text.index("z"))  # ValueError

Note

⚠️ Use find() when you want to avoid errors.

3. count()

Counts how many times a substring appears.

count.py

print("banana".count("a"))   # 3

3. Modifying Strings ✂️

1. replace()

Replaces part of the string with something else.

replace.py

text = "I love Java"
print(text.replace("Java", "Python"))

2. strip(), lstrip(), rstrip()

Remove whitespace from strings.

strip.py

msg = "   hello python   "
print(msg.strip())   # removes both sides
print(msg.lstrip())  # removes left spaces
print(msg.rstrip())  # removes right spaces

3. split()

Splits a string into a list.

split.py

msg = "hello python world"
words = msg.split()
print(words)   # ['hello', 'python', 'world']

4. join()

Joins elements of a list into a string.

join.py

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

4. Checking String Properties ✔️

1. isalpha()

Checks if the string contains only letters.

isalpha.py

print("Python".isalpha())   # True
print("Python3".isalpha())  # False

2. isdigit()

isdigit.py

print("12345".isdigit())   # True
print("12a3".isdigit())     # False

3. isalnum()

isalnum.py

print("Python3".isalnum())   # True
print("Hello!".isalnum())     # False

4. isspace()

isspace.py

print("   ".isspace())   # True
print(" hi ".isspace())          # False

5. startswith() & endswith()

startswith_endswith.py

text = "hello python"
print(text.startswith("hello"))   # True
print(text.endswith("python"))    # True

5. String Formatting Functions ✨

1. format()

format.py

name = "Sathish"
age = 25
print("My name is {} and I am {} years old".format(name, age))

2. f-strings (modern & recommended)

fstring.py

print(f"Hello {'Python'.upper()}!")

3. % formatting (old style)

percent_style.py

print("Value = %d" % 50)

6. Useful Character Tests 🎯

char_tests.py

ch = "A"
print(ch.isupper())
print(ch.islower())
print(ch.isnumeric())

7. Real-World Example 🌍

real_world.py

text = input("Enter a sentence: ")

print("Uppercase:", text.upper())
print("Word count:", len(text.split()))
print("Replaced:", text.replace("a", "@"))

Conclusion 🎉

>>“String functions are your toolbox — with them, text becomes easy to shape, clean, and control.” 🔥

You now master the most powerful string functions in Python. Want to continue with Operators, Expressions, Conditional Statements, Loops, Functions, Lists, Tuples, or Dictionaries? Just tell me — I’ll build the next tutorial! 😊