HOME

string-methods-in-python-explained-by-Gagan-Khanna

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.

string-methods-in-python-explained-by-Gagan-Khanna

string-methods-in-python-explained-by-Gagan-Khanna

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. , Apr-02-2026

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.


1. Case Transformation Methods

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).

Python
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"

2. Searching and Counting

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.

Python
phrase = "To be, or not to be"

print(phrase.find("be"))     # 3
print(phrase.count("be"))    # 2
print(phrase.startswith("To")) # True

3. Cleaning and Stripping

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.

Python
user_input = "   hello world   "
print(user_input.strip()) # "hello world"

data = "---important---"
print(data.strip("-"))    # "important"

4. Splitting and Joining

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.

Python
# 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"

5. Replacing and Formatting

  • 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.

Python
msg = "I like Java"
print(msg.replace("Java", "Python")) # "I like Python"

# zfill example
code = "42"
print(code.zfill(5)) # "00042"

6. Boolean Validation Methods

These return True or False based on the content of the string.

MethodReturns 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()

Comments 0


string-methods-in-python-explained-by-Gagan-Khanna

This is a tutorial on string methods in python , explained through various examples By Gagan Khanna . For online class contact 9312352488


Popular

  • Learn Python Online
  • Online Python Classes
  • Gagan Khanna
  • 9312352488

Esmio...

Teachlive Flagship Program of Esmiohosting.com

Aaja.in

Boost via Reels & Images

Citylead

Publish Business Articles

Quick...

Professional Help for Growth

Tags

Learn Python Online Online Python Classes Gagan Khanna 9312352488 Python String Methods Programming Coding Tutorials Python Strings


Topics You may be intrested in










































































































Teachlive is a platform dedicated to both teachers and students, providing free resources to help spread knowledge and benefit students in their educational journey. Under guidence of Seema Khanna ,at Teachlive, we offer much more than just academic support; we help students develop critical life skills, offer parental guidance, and keep everyone informed about the latest changes in the Indian education system.


Please Take a while to visit our sponsors website Shopeze.in - Home Decoration Items