Python strings are immutable, meaning once a string is created, it cannot be changed. However, Python provides a rich set of built-in methods that allow you to manipulate string data and return new string objects.

Python strings are immutable, meaning once a string is created, it cannot be changed. However, Python provides a rich set of built-in methods that allow you to manipulate string data and return new string objects.
These methods change the casing of the string. They are frequently used for normalizing user input.
lower() / upper(): Converts all characters to lowercase or uppercase.
capitalize(): Capitalizes only the first character of the string.
title(): Capitalizes the first letter of every word (title case).
swapcase(): Inverts the case (lower becomes upper and vice versa).
text = "python is FUN"
print(text.lower()) # "python is fun"
print(text.upper()) # "PYTHON IS FUN"
print(text.capitalize()) # "Python is fun"
print(text.title()) # "Python Is Fun"
Use these methods to locate substrings or determine how many times a pattern appears.
find(sub): Returns the lowest index where the substring sub is found. Returns -1 if not found.
index(sub): Similar to find(), but raises a ValueError if the substring is not found.
count(sub): Returns the number of non-overlapping occurrences of the substring.
startswith(prefix) / endswith(suffix): Returns True if the string begins or ends with the specified value.
phrase = "To be, or not to be"
print(phrase.find("be")) # 3
print(phrase.count("be")) # 2
print(phrase.startswith("To")) # True
When dealing with data from files or web forms, strings often contain unwanted whitespace.
strip(): Removes whitespace (or specific characters) from both ends of the string.
lstrip() / rstrip(): Removes whitespace only from the left or right side.
user_input = " hello world "
print(user_input.strip()) # "hello world"
data = "---important---"
print(data.strip("-")) # "important"
These are arguably the most powerful methods for data parsing.
split(sep): Breaks the string into a list of strings based on the separator sep. If no separator is provided, it splits by any whitespace.
join(iterable): The inverse of split(). it takes a list/tuple of strings and concatenates them using the string it was called on as a connector.
# Splitting
csv_data = "apple,banana,cherry"
fruits = csv_data.split(",") # ['apple', 'banana', 'cherry']
# Joining
separator = " | "
new_string = separator.join(fruits)
print(new_string) # "apple | banana | cherry"
replace(old, new): Replaces all occurrences of a substring with another.
format(): Used for string interpolation (though f-strings are now more common in Python 3.6+).
zfill(width): Adds zeros at the beginning of the string until it reaches a specified length. Useful for formatting numbers.
msg = "I like Java"
print(msg.replace("Java", "Python")) # "I like Python"
# zfill example
code = "42"
print(code.zfill(5)) # "00042"
These return True or False based on the content of the string.
| Method | Returns True if... |
isalpha() | All characters are alphabetic. |
isdigit() | All characters are digits. |
isalnum() | String is alphanumeric (no symbols). |
isspace() | String contains only whitespace. |
islower() / isupper() | String is entirely lower/upper case. |
Note on Immutability: Always remember that string methods do not modify the original string. You must assign the result to a new variable or overwrite the old one:
text = text.strip()

This is a tutorial on string methods in python , explained through various examples By Gagan Khanna . For online class contact 9312352488
Learn Python Online Online Python Classes Gagan Khanna 9312352488 Python String Methods Programming Coding Tutorials Python Strings