0% found this document useful (0 votes)
88 views4 pages

Class 11 String Manipulation Notes

Uploaded by

Vimala Rajendran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views4 pages

Class 11 String Manipulation Notes

Uploaded by

Vimala Rajendran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Class 11 Computer Science Notes – String Manipulation (Python)

SHORT NOTES (For Quick Revision)

1. **Definition of String:**
A string is a sequence of characters enclosed within single, double, or triple quotes
in Python.

2. **String Creation:**
Examples:
a = 'Hello'
b = "Python"
c = '''Welcome'''

3. **String Indexing:**
- Positive Indexing starts from 0
- Negative Indexing starts from -1
Example:
s = "Python" → s[0] = 'P', s[-1] = 'n'

4. **String Operators:**
- `+` → Concatenation
- `*` → Repetition
- `in` / `not in` → Membership testing
- Example: "Py" + "thon" = "Python"

5. **String Slicing:**
Syntax: string[start : end : step]
Example: s = "Python" → s[1:4] = 'yth'

6. **String Functions:**
len(), max(), min(), str(), sorted()

7. **String Methods:**
upper(), lower(), title(), capitalize(), count(), find(), replace(), isalpha(), isdigit(),
isspace()

8. **String Traversal:**
Traversing means accessing each character in the string, often using loops.
Example:
for ch in "Hello":
print(ch)
9. **Immutability of Strings:**
Strings cannot be modified after creation. Any change creates a new string.

DETAILED NOTES (With Examples and Explanations)

1. Introduction to Strings
A string is a collection of characters enclosed in quotes. It is one of the most
commonly used data types in Python.
Example:
s1 = 'Hello'
s2 = "Welcome"
s3 = '''Python Programming'''

2. String Indexing and Slicing


- Indexing allows access to individual characters.
Example: s = "Python"
s[0] = 'P', s[5] = 'n', s[-1] = 'n'
- Slicing extracts a portion of the string: s[start:end:step]
Example: s[1:4] → 'yth'

3. String Operators
| Operator | Description | Example | Result |
|-----------|--------------|----------|---------|
| + | Concatenation | 'Good' + 'Morning' | 'GoodMorning' |
| * | Repetition | 'Hi' * 3 | 'HiHiHi' |
| in | Membership | 'a' in 'data' | True |
| not in | Non-membership | 'z' not in 'data' | True |

4. String Functions
- len(string): returns length
- max(string): returns max ASCII character
- min(string): returns min ASCII character
- sorted(string): returns characters in sorted order

5. String Methods
| Method | Description | Example |
|---------|-------------|----------|
| upper() | Converts to uppercase | 'hello'.upper() → 'HELLO' |
| lower() | Converts to lowercase | 'HELLO'.lower() → 'hello' |
| title() | Capitalizes first letter of each word | 'hello world'.title() → 'Hello World' |
| count(x) | Counts occurrences of x | 'banana'.count('a') → 3 |
| find(x) | Returns index of first occurrence | 'data'.find('a') → 1 |
| replace(x, y) | Replaces substring | 'hello'.replace('h','H') → 'Hello' |
| isalpha() | True if all characters are alphabets | 'abc'.isalpha() → True |
| isdigit() | True if all are digits | '123'.isdigit() → True |
| isspace() | True if all are spaces | ' '.isspace() → True |

6. String Traversal
You can iterate through each character in a string using loops.
Example:
for I in "Python":
print(I)

7. Immutability of Strings
Strings are immutable — once created, their content cannot be changed.
Example:
s = "Hello"
s[0] = 'Y' # Error

8. Common String Programs


1. Program to count no. of vowels and consonants in a string
str1 = input("Enter a string: ")
vowels = 0
consonants = 0
vowels = "aeiouAEIOU"
for ch in str1:
if [Link](): # Check if the character is a letter
if ch in vowel_set:
vowels += 1
else:
consonants += 1
print("Number of vowels:", vowels)
print("Number of consonants:", consonants)

2. Program to check palindrome


str1 = input("Enter a string: ")

# Convert to lowercase to make the check case-insensitive


str1 = [Link]()
# Reverse the string using slicing
rev_str = str1[::-1]
if str1 == rev_str:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
3. Program to reverse a string without reverse function
str1 = input("Enter a string: ")
rev = " "
for ch in str1:
rev = ch + rev # add each character at the beginning
print("Reversed string:", rev)

You might also like