Subject: Computer Science(083) Chapter: String in Python CBSE Class :XI & XII
This single post is enough to understand the entire chapter — Strings in Python. I’ve explained all the important concepts, examples, and commonly asked board exam questions in a simple way.
Try to answer each example on your own before checking the answer. If you need any clarification or help with a question, feel free to text me anytime. Let’s finish this chapter together with full confidence!
A practice worksheet based on CBSE Class 11 and 12 Python String Manipulation is available in this post to help you revise key concepts through exam-oriented questions. This worksheet includes output-based questions, function usage, and logic-building exercises exactly as expected in board exams. Make sure to download and use it effectively for self-practice and to strengthen your understanding of string functions and operations.
Accessing Individual Characters in a String (Indexing)
>>>M=”Python”
>>>M[0]
>>>
>>>M[4]
>>>
>>>M[-3]
>>>
>>>M[-9]
>>>
>>>M[20]
String Operation
1.Slicing
>>>s="Computer System"
>>>s[0:5]
>>>
>>>s[0:]
>>>
>>>s[3:]
>>>
>>>s[:8]
>>>
>>>s[:]
>>>
>>>s[::2]
>>>
>>>s[::]
>>>
>>>s[10:3:-1]
>>>
>>>s[10:3]
>>>
>>>s[-6:-2]
>>>
>>>s[-6:-20:2]
>>>
>>>s[-6:-20:-2]
>>>
>>>s[5:60]
>>>
>>>s[60:]
>>>
>>>s[60::-1]
>>>
>>>s[0:0]
>>>
>>>s[::0]
>>>
2.Membership
>>>'C' in 'car'
>>>
>>>'c' in 'car'
>>>
>>>'car' in 'c'
>>>
>>>'car' not in 'c'
>>>
>>>'c' not in 'car'
>>>
>>>'C' not in ‘car'
>>>
>>>a="apple"
>>>'e' in a
>>>
>>>a in 'e'
>>>
3.Concatenation
>>>"hello"+'Hi'
>>>
>>>"hello"+"Welcome"
>>>
>>>"hello"+" "+"Welcome"
>>>
>>>a="python"
>>>b="program"
>>>a+b
>>>
>>>'hi'+10
>>>
>>>'python'+'3.10'
>>>
>>>a="apple"
>>>a*2
>>>
4.Repetition
>>>'Hi'*3
>>>
>>>2*'hello'
>>>
>>>2*2*'hi'
>>>
>>>"Hi! "*2*3
>>>
>>>'Hi'*'hello'
5.Reverse
>>>s=”python”
>>>s[::-1]
>>>
6.Comparition
>>>s1 = "apple"
>>>s2 = "banana"
>>>print(s1 == s2)
>>>print(s1 != s2)
>>>print(s1 < s2)
>>>print(s1 < =s2)
String Methods and Function
1.len()
# Example 1: Simple string
>>>name = "Hello"
>>>print(len(name))
>>>
# Example 2: String with spaces
>>>sentence = "Hello World"
>>>print(len(sentence))
>>>
# Example 3: Empty string
>>>empty = ""
>>>print(len(empty))
>>>
# Example 4: String with numbers and special characters
>>>mixed = "Python3.8!"
>>>print(len(mixed))
>>>
# Example 5: Multiline string
>>>multiline = """Hello \
World"""
>>>print(len(multiline))
# Example 6: String with tabs and spaces
>>>tabs_spaces = "Hi\t there "
>>>print(len(tabs_spaces))
2.Capitalize()
# Example 1: Normal lowercase string
text = "hello world"
print(text.capitalize())
# Example 2: String with uppercase letters in middle
text2 = "pYTHON programming"
print(text2.capitalize())
# Example 3: String starting with a number or special character
text3 = "123abc"
print(text3.capitalize())
# Example 4: String starting with uppercase letter already
text4 = "Hello"
print(text4.capitalize())
# Example 5: Empty string
empty = ""
print(empty.capitalize())
# Example 6: String with spaces at start
text5 = " hello"
print(text5.capitalize())
3.istitle()
# Example 1: Proper title case string
text1 = "Hello World"
print(text1.istitle())
# Example 2: Not title case because 'world' is lowercase
text2 = "Hello world"
print(text2.istitle())
# Example 3: All uppercase letters
text3 = "HELLO WORLD"
print(text3.istitle())
# Example 4: Empty string
text4 = ""
print(text4.istitle())
# Example 5: Single word in title case
text5 = "Python"
print(text5.istitle())
# Example 6: String with numbers and special characters
text6 = "Chapter 1: Introduction"
print(text6.istitle())
4.title()
# Example 1: Simple sentence
text1 = "hello world"
print(text1.title())
# Example 2: Mixed case string
text2 = "pyTHon ProGRAMming"
print(text2.title())
# Example 3: String with numbers and punctuation
text3 = "chapter 1: introduction to python"
print(text3.title())
# Example 4: String already in title case
text4 = "Data Science"
print(text4.title())
# Example 5: Empty string
empty = ""
print(empty.title())
5.swapcase()
# Example 1: Mixed case string
text1 = "Hello World"
print(text1.swapcase())
# Example 2: All uppercase string
text2 = "PYTHON"
print(text2.swapcase())
# Example 3: All lowercase string
text3 = "programming"
print(text3.swapcase())
# Example 4: String with numbers and special characters
text4 = "Python 3.8!"
print(text4.swapcase())
# Example 5: Empty string
empty = ""
print(empty.swapcase())
6.isalpha()
# Example 1: Only letters
text1 = "Python"
print(text1.isalpha())
# Example 2: Letters and spaces
text2 = "Hello World"
print(text2.isalpha())
# Example 3: Letters and numbers
text3 = "Python3"
print(text3.isalpha())
# Example 4: Empty string
text4 = ""
print(text4.isalpha())
# Example 5: Only lowercase letters
text5 = "programming"
print(text5.isalpha())
# Example 6: Only uppercase letters
text6 = "DATA"
print(text6.isalpha())
7.isalnum()
# Example 1: Only letters
text1 = "Python"
print(text1.isalnum())
# Example 2: Letters and numbers
text2 = "Python3"
print(text2.isalnum())
# Example 3: Letters, numbers and space
text3 = "Python 3"
print(text3.isalnum())
# Example 4: Letters and special characters
text4 = "Hello!"
print(text4.isalnum())
# Example 5: Empty string
text5 = ""
print(text5.isalnum())
# Example 6: Only numbers
text6 = "12345"
print(text6.isalnum())
8.isdigit()
# Example 1: Only digits
text1 = "12345"
print(text1.isdigit())
# Example 2: Digits with letters
text2 = "123abc"
print(text2.isdigit())
# Example 3: Digits with spaces
text3 = "123 456"
print(text3.isdigit())
# Example 4: Empty string
text4 = ""
print(text4.isdigit())
# Example 5: Digits with special characters
text5 = "123-456"
print(text5.isdigit())
# Example 6: Single digit
text6 = "7"
print(text6.isdigit())
9.isspace()
# Example 1: Only spaces
text1 = " "
print(text1.isspace())
# Example 2: Spaces and tabs
text2 = " \t "
print(text2.isspace())
# Example 3: Spaces and letters
text3 = " a "
print(text3.isspace())
# Example 4: Empty string
text4 = ""
print(text4.isspace())
# Example 5: Newline character
text5 = "\n"
print(text5.isspace())
# Example 6: Spaces and digits
text6 = " 123 "
print(text6.isspace())
# Example 7: Character,Spaces and digits
text6 = " Hi 123 "
print(text6.isspace())
10.lower()
# Example 1: String with uppercase letters
text1 = "Hello World"
print(text1.lower())
# Example 2: String already in lowercase
text2 = "python"
print(text2.lower())
# Example 3: String with mixed case and numbers
text3 = "Python3.8"
print(text3.lower())
# Example 4: String with special characters
text4 = "WELCOME!"
print(text4.lower())
# Example 5: Empty string
empty = ""
print(empty.lower())
11.islower()
# Example 1: All lowercase letters
text1 = "hello world"
print(text1.islower())
# Example 2: Mixed case letters
text2 = "Hello World"
print(text2.islower())
# Example 3: String with no letters
text3 = "12345"
print(text3.islower())
# Example 4: String with lowercase letters and digits
text4 = "python3"
print(text4.islower())
# Example 5: Empty string
text5 = ""
print(text5.islower())
# Example 6: Lowercase letters with punctuation
text6 = "hello!"
print(text6.islower())
12.upper()
# Example 1: String with lowercase letters
text1 = "hello world"
print(text1.upper())
# Example 2: String already in uppercase
text2 = "PYTHON"
print(text2.upper())
# Example 3: String with mixed case and numbers
text3 = "Python3.8"
print(text3.upper())
# Example 4: String with special characters
text4 = "welcome!"
print(text4.upper())
# Example 5: Empty string
empty = ""
print(empty.upper())
13.isupper()
# Example 1: All uppercase letters
text1 = "HELLO WORLD"
print(text1.isupper())
# Example 2: Mixed case letters
text2 = "Hello World"
print(text2.isupper())
# Example 3: String with no letters
text3 = "12345"
print(text3.isupper())
# Example 4: String with uppercase letters and digits
text4 = "PYTHON3"
print(text4.isupper())
# Example 5: Empty string
text5 = ""
print(text5.isupper())
# Example 6: Uppercase letters with punctuation
text6 = "HELLO!"
print(text6.isupper())
14.find()
# Example 1: Substring found
text1 = "Hello, welcome to Python programming"
print(text1.find("welcome"))
# Example 2: Substring not found
text2 = "Hello, world!"
print(text2.find("Python"))
# Example 3: Find substring with start index
text3 = "banana"
print(text3.find("a"))
print(text3.find("a", 2))
# Example 4: Find substring with start and end index
text4 = "apple apple apple"
print(text4.find("apple", 6, 15))
# Example 5: Empty substring
text5 = "Hello"
print(text5.find(""))
15.count()
# Example 1: Count substring occurrences
text1 = "banana"
print(text1.count("a"))
# Example 2: Count substring that does not exist
text2 = "hello world"
print(text2.count("z"))
# Example 3: Count substring with start and end
text3 = "hello hello hello"
print(text3.count("hello", 6))
print(text3.count("hello", 0, 10))
# Example 4: Count spaces
text4 = "I am learning Python"
print(text4.count(" "))
# Example 5: Count empty substring
text5 = "abc"
print(text5.count(""))
16.index()
# Example 1: Substring found
text1 = "Hello, welcome to Python programming"
print(text1.index("welcome"))
# Example 2: Substring not found — raises error
text2 = "Hello, world!"
# print(text2.index("Python"))
# Example 3: Find substring with start index
text3 = "banana"
print(text3.index("a"))
print(text3.index("a", 2))
# Example 4: Find substring with start and end index
text4 = "apple apple apple"
print(text4.index("apple", 6, 15))
# Example 5: Empty substring
text5 = "Hello"
print(text5.index(""))
17.strip()
# Example 1: Remove spaces from start and end
text1 = " Hello World "
print(text1.strip())
# Example 2: Remove tabs and newlines
text2 = "\t\nPython Programming\n\t"
print(text2.strip())
# Example 3: Remove specific characters
text3 = "xxxyHello Worldyyyxx"
print(text3.strip("xy"))
# Example 4: String with no leading/trailing spaces
text4 = "Data Science"
print(text4.strip())
# Example 5: Empty string
empty = ""
print(empty.strip())
18.lstrip()
# Example 1: Remove spaces from the start
text1 = " Hello World "
print(text1.lstrip())
# Example 2: Remove tabs and newlines from the start
text2 = "\t\nPython Programming\n\t"
print(text2.lstrip())
# Example 3: Remove specific characters from the start
text3 = "xxxyHello Worldyyyxx"
print(text3.lstrip("xy"))
# Example 4: String with no leading spaces
text4 = "Data Science"
print(text4.lstrip())
# Example 5: Empty string
empty = ""
print(empty.lstrip())
19.rstrip()
# Example 1: Remove spaces from the end
text1 = " Hello World "
print(text1.rstrip())
# Example 2: Remove tabs and newlines from the end
text2 = "\t\nPython Programming\n\t"
print(text2.rstrip())
# Example 3: Remove specific characters from the end
text3 = "xxxyHello Worldyyyxx"
print(text3.rstrip("xy"))
# Example 4: String with no trailing spaces
text4 = "Data Science"
print(text4.rstrip())
# Example 5: Empty string
empty = ""
print(empty.rstrip())
20.split()
# Example 1: Split by default whitespace
text1 = "I am learning Python"
print(text1.split())
# Example 2: Split by comma
text2 = "apple,banana,orange"
print(text2.split(","))
# Example 3: Split by space with maxsplit
text3 = "one two three four"
print(text3.split(" ", 2))
# Example 4: Split by character
text4 = "a-b-c-d"
print(text4.split("-"))
# Example 5: String with no separator found
text5 = "HelloWorld"
print(text5.split(","))
21.join()
# Example 1: Join words with space
words = ["I", "am", "learning", "Python"]
sentence = " ".join(words)
print(sentence)
# Example 2: Join items with comma
fruits = ["apple", "banana", "orange"]
result = ",".join(fruits)
print(result)
# Example 3: Join characters with no separator
chars = ["a", "b", "c", "d"]
joined = "".join(chars)
print(joined)
# Example 4: Join lines with newline character
lines = ["Line 1", "Line 2", "Line 3"]
text = "\n".join(lines)
print(text)
# Example 5: Joining an empty list
empty_list = []
print(",".join(empty_list))
22.partition()
# Example 1: Separator is found
text1 = "Python is fun"
result = text1.partition("is")
print(result)
# Example 2: Separator at the beginning
text2 = "Hello world"
print(text2.partition("Hello"))
# Example 3: Separator at the end
text3 = "Good morning"
print(text3.partition("morning"))
# Example 4: Separator not found
text4 = "Learn Python"
print(text4.partition("Java"))
# Example 5: Using a space as separator
text5 = "apple banana cherry"
print(text5.partition(" "))
23.startswith()
# Example 1: Basic usage
text1 = "Python is fun"
print(text1.startswith("Python"))
# Example 2: Case-sensitive check
text2 = "Hello World"
print(text2.startswith("hello"))
# Example 3: Check with start index
text3 = "Welcome to Python"
print(text3.startswith("to", 8))
# Example 4: Using both start and end
text4 = "Learn Coding Now"
print(text4.startswith("Coding", 6, 13))
# Example 5: Substring not at start
text5 = "apple pie"
print(text5.startswith("pie"))
24.endswith()
# Example 1: Basic usage
text1 = "Python is fun"
print(text1.endswith("fun"))
# Example 2: Case-sensitive
text2 = "Hello World"
print(text2.endswith("world"))
# Example 3: Using start and end
text3 = "Welcome to coding"
print(text3.endswith("to", 0, 11))
# Example 4: Not ending with substring
text4 = "Learn Python"
print(text4.endswith("Java"))
# Example 5: Empty string check
text5 = "Test"
print(text5.endswith(""))
25.ord()
# Example 1: Capital letter
print(ord('A'))
# Example 2: Small letter
print(ord('a'))
# Example 3: Digit
print(ord('0'))
# Example 4: Special character
print(ord('@'))
# Example 5: Space character
print(ord(' '))
26.chr()
# Example 1: Capital letter
print(chr(65))
# Example 2: Small letter
print(chr(97))
# Example 3: Digit character
print(chr(48))
# Example 4: Special character
print(chr(64))
# Example 5: Space character
print(chr(32))

0 Comments