CHAPTER-8
STRINGS
1) What is String?
• A string is a sequence of characters.
• Characters may be letters, digits, spaces, or symbols.
• Strings are enclosed in:
Single quotes ' '
Double quotes " "
Triple quotes ''' ''' or """ """ (used for multi-line strings)
Example
s1 = 'Hello'
s2 = "Python"
s3 = """Welcome to Python"""
2) Accessing Characters in a String (Indexing)
• Each character has an index number.
• Index starts from 0 (left side).
• Last character index = length – 1
• Negative indexing is allowed (from right side).
Example
str1 = "Hello"
• str1[0] # H
• str1[-1] # o
3) Why String is immutable?
A string is an immutable data type. It means that the contents of the string cannot be
changed after it has been created. An attempt to do this would lead to
an error.
>>> str1 = "Hello World!"
#if we try to replace character 'e' with 'a'
>>> str1[1] = 'a'
TypeError: 'str' object does not support item assignment
4) Explain String operations.
a) Concatination
Concatenation means joining two or more strings into a single string.
str1 = "Hello"
str2 = "World"
result = str1 + str2
print(result) Outout - Hello World
b) Repetition
Repetition means repeating a string multiple times.
str1 = "Hi"
print(str1 * 3) Output - HiHiHi
c) Membership(in and not in)
Membership operators are used to check whether a substring exists inside a string.
i)in Operator
Returns True if the substring is found.
str1 = "Hello World"
print("World" in str1) Output- True
ii) not in Operator
Returns True if the substring is NOT found.
print("Python" not in str1) Output = True
d) Slicing
Slicing is used to extract a part (substring) from a string.
Syntax
string[start : end : step]
str1 = "Hello World"
print(str1[0:5]) Output - Hello
print(str1[:5]) Output – Hello
print(str1[6:]) Output – World
5) Write a python program to traverse a string Hello using for loop
for ch in "Hello": for ch in "Hello":
print(ch,end=’’) print(ch)
Output Output
Hello H
e
Or l
l
o
6) Write a python program to traverse a string Hello using while loop
i=0
while i < len("Hello"):
print("Hello"[i],end=’’) OR
i += 1
i=0
Output while i < len("Hello"):
Hello print("Hello"[i],end=’’)
i += 1 e
l
Output l
H o
7) String Methods – Definition & Examples
1) upper()
Definition:
Converts all lowercase letters in a string to uppercase.
Example:
s = "Hello World"
print([Link]())
Output:
HELLO WORLD
2) lower()
Definition:
Converts all uppercase letters in a string to lowercase.
Example:
s = "Hello World"
print([Link]())
Output:
hello world
3) title()
Definition:
Converts the first letter of each word to uppercase and the rest to lowercase.
Example:
s = "welcome to python"
print([Link]())
Output:
Welcome To Python
4) count()
Definition:
Returns the number of times a substring appears in a string.
Example:
s = "hello hello world"
print([Link]("hello"))
Output:
2
5) find()
Definition:
Returns the index of the first occurrence of a substring.
Returns -1 if not found.
Example:
s = "Hello World"
print([Link]("World"))
Output:
6
print([Link]("is"))
Output:
-1
6) startswith()
Definition:
Checks whether the string starts with a given substring.
Returns True or False.
Example:
s = "Python Programming"
print([Link]("Python"))
Output:
True
7) endswith()
Definition:
Checks whether the string ends with a given substring.
Example:
s = "[Link]"
print([Link](".txt"))
Output:
True
8) replace()
Definition:
Replaces all occurrences of a substring with another substring.
Example:
s = "Hello World"
print([Link]("World", "Python"))
Output:
Hello Python
9) strip()
Definition:
Removes leading and trailing spaces from a string.
Example:
s = " Hello World "
print([Link]())
Output:
Hello World
10) split()
Definition:
Splits a string into a list of words based on a delimiter (default: space).
Example:
s = "Python is easy"
print([Link]())
Output:
['Python', 'is', 'easy']
11) join()
Definition:
Joins characters of a string (or elements of a list) using a separator.
Example:
s = "-"
print([Link]("HELLO"))
Output:
H-E-L-L-O
12) isalnum()
Definition:
Returns True if the string contains only alphabets and digits.
Example:
s = "Hello123"
print([Link]())
Output:
True
13) isupper()
Definition:
Returns True if all letters are uppercase.
Example:
s = "HELLO"
print([Link]())
Output:
True
14) islower()
Definition:
Returns True if all letters are lowercase.
Example:
s = "hello"
print([Link]())
Output:
True
15) isspace()
Definition:
Returns True if the string contains only whitespace characters.
Example:
s=" "
print([Link]())
Output:
True
Important Questions
A. Very Short Answer (1 Mark)
1. What is a string in Python?
2. Which function returns the length of a string?
3. Is string mutable or immutable?
4. What is the index of the first character?
B. Short Answer (2–3 Marks)
1. Explain positive and negative indexing with example.
2. Write the syntax of slicing.
3. What is string traversal?
C. Long Answer (5 Marks)
1. Write any 5 string methods with use.
2. Write a python program to traverse a string Hello using for loop and while loop.