Here is a clear, complete, and exam-ready explanation of Strings in Python, perfect for BCA/MCA/B.Tech students.
Strings in Python
A string in Python is a sequence of characters enclosed in quotes.
Python strings are:
✔ Ordered
✔ Immutable (cannot be changed)
✔ Iterable (can be looped through)
✔ Support indexing and slicing
Strings are widely used for storing text, names, messages, and more.
1. Creating Strings
Strings can be created using:
Single quotes
s = 'Hello'
Double quotes
s = "Python"
Triple quotes (for multi-line strings)
s = """This is
a multi-line
string."""
Triple quotes (''' or """) also allow including quotes inside strings.
2. Accessing Characters (Indexing)
Python uses 0-based indexing.
s = "Python"
print(s[0]) # P
print(s[3]) # h
Negative indexing:
print(s[-1]) # n
print(s[-3]) # h
3. Slicing Strings
Used to extract part (substring) of a string.
Syntax:
string[start : end : step]
Examples:
s = "Python"
print(s[1:4]) # yth
print(s[:3]) # Pyt
print(s[3:]) # hon
print(s[::-1]) # Reverse string -> nohtyP
4. Strings Are Immutable
You cannot change characters in a string.
s = "Hello"
s[0] = "h" # ❌ Error: strings are immutable
To modify, create a new string:
s = "Hello"
s = "h" + s[1:]
5. String Concatenation
Use + operator:
a = "Hello"
b = "World"
c = a + " " + b
6. String Repetition
Use * operator:
s = "Hi"
print(s * 3) # HiHiHi
7. Looping Through Strings
for ch in "Python":
print(ch)
8. Membership Operators
s = "Python Programming"
print("Python" in s) # True
print("Java" not in s) # True
9. Useful String Functions and Methods
📌 1. len()
Returns length of string.
len("Python") # 6
📌 2. lower()
Converts to lowercase.
"HELLO".lower() # hello
📌 3. upper()
Converts to uppercase.
"hello".upper() # HELLO
📌 4. capitalize()
Capitalizes first letter.
"python".capitalize() # Python
📌 5. title()
Capitalizes first letter of each word.
"hello world".title()
📌 6. strip(), lstrip(), rstrip()
Removes spaces.
" hello ".strip() # hello
📌 7. find()
Returns index of substring (or -1).
"hello".find("l") # 2
📌 8. replace()
Replaces substring.
"Python".replace("Py", "My") # Mython
📌 9. split()
Splits string into list.
"apple,banana,mango".split(",")
📌 10. join()
Joins a list into a string.
"-".join(["a", "b", "c"]) # a-b-c
📌 11. isdigit(), isalpha(), isalnum()
"123".isdigit() # True
"abc".isalpha() # True
"abc123".isalnum() # True
10. String Formatting
Using f-strings (best method)
name = "John"
age = 20
print(f"My name is {name} and I am {age} years old")
Using format()
"{} is {}".format("Python", "fun")
11. Escape Characters
Used to insert special characters inside strings.
| Escape Sequence | Meaning |
|---|---|
\' | Single quote |
\" | Double quote |
\\ | Backslash |
\n | New line |
\t | Tab |
Example:
print("Hello\nWorld")
12. Raw Strings
Prefix r to treat backslashes literally.
s = r"C:\Users\Name"
print(s)
13. Unicode Strings
Python strings are Unicode by default, so they support all languages.
s = "नमस्ते"
14. Examples of String Programs
Example 1: Count vowels
s = "Education"
v = 0
for ch in s.lower():
if ch in "aeiou":
v += 1
print(v)
Example 2: Reverse a string
s = "Python"
print(s[::-1])
Example 3: Check palindrome
s = "madam"
print(s == s[::-1]) # True
Summary Table: Strings
| Property | Description |
|---|---|
| Type | Sequence |
| Mutable | ❌ No (immutable) |
| Ordered | ✔ Yes |
| Supports slicing | ✔ Yes |
| Allows repetition | ✔ Yes |
| Supports concatenation | ✔ Yes |
