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
1. Changing Case 🔤
1. upper()
Converts the entire string to uppercase.
upper.py
text = "hello python"
print(text.upper()) # HELLO PYTHON2. lower()
lower.py
print("HELLO".lower()) # hello3. title()
Capitalizes the first letter of every word.
title.py
print("hello world".title()) # Hello World4. capitalize()
Capitalizes only the first letter of the string.
capitalize.py
print("python basics".capitalize()) # Python basics5. swapcase()
Swaps uppercase ↔ lowercase.
swapcase.py
print("PyThOn".swapcase()) # pYtHoN2. 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")) # -12. 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")) # ValueErrorNote
3. count()
Counts how many times a substring appears.
count.py
print("banana".count("a")) # 33. 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 spaces3. 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-world4. Checking String Properties ✔️
1. isalpha()
Checks if the string contains only letters.
isalpha.py
print("Python".isalpha()) # True
print("Python3".isalpha()) # False2. isdigit()
isdigit.py
print("12345".isdigit()) # True
print("12a3".isdigit()) # False3. isalnum()
isalnum.py
print("Python3".isalnum()) # True
print("Hello!".isalnum()) # False4. isspace()
isspace.py
print(" ".isspace()) # True
print(" hi ".isspace()) # False5. startswith() & endswith()
startswith_endswith.py
text = "hello python"
print(text.startswith("hello")) # True
print(text.endswith("python")) # True5. 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 🎉
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! 😊