Python String Operations | Concatenation, Repetition, Membership & Slicing | CBSE Class 11 Computer Science
Class 11 · Computer Science
Python String Operations
Python provides several operations that make working with strings simple and efficient. These operations allow us to combine strings, repeat strings, check the presence of characters or words, and extract required portions of a string. Understanding these operations is essential for solving many programming problems in CBSE Class 11 Computer Science.
Learning Objectives
- Understand different string operations.
- Perform string concatenation and repetition.
- Use membership operators.
- Extract characters using slicing.
- Understand positive and negative slicing.
String Concatenation
Concatenation means joining two or more strings using the + operator.
Syntax
string1 + string2
Example 1
first="Code"
second="Step"
print(first+second)
Output
CodeStep
Example 2
first="Code"
second="Step Academy"
print(first+" "+second)
Output
Code Step Academy
String Repetition
The * operator repeats a string a specified number of times.
Syntax
string * number
Example
print("Python "*3)
Output
Python Python Python
Membership Operators
Python provides two membership operators:
innot in
These operators check whether a character or substring is present in a string.
Using in Operator
text="Computer Science"
print("Science" in text)
print("Math" in text)
Output
True
False
Using not in Operator
text="Python Programming"
print("Java" not in text)
print("Python" not in text)
Output
True
False
String Slicing
Slicing is used to extract a part of a string.
Syntax
string[start:stop]
The character at the start index is included, whereas the character at the stop index is excluded.
Example String
word="COMPUTER"
| Character | C | O | M | P | U | T | E | R |
|---|---|---|---|---|---|---|---|---|
| Positive Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| Negative Index | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
Example 1
word="COMPUTER"
print(word[0:4])
Output
COMP
Example 2
word="COMPUTER"
print(word[2:6])
Output
MPUT
Example 3
word="COMPUTER"
print(word[:5])
Output
COMPU
Example 4
word="COMPUTER"
print(word[3:])
Output
PUTER
Example 5
word="COMPUTER"
print(word[:])
Output
COMPUTER
Negative Slicing
Negative indexing allows slicing from the end of the string.
Example
word="COMPUTER"
print(word[-5:-1])
Output
PUTE
Slicing with Step
The third value specifies the step or increment.
Syntax
string[start:stop:step]
Example 1
word="COMPUTER"
print(word[0:8:2])
Output
CMUE
Example 2
word="COMPUTER"
print(word[::2])
Output
CMUE
Example 3
word="COMPUTER"
print(word[::-1])
Output
RETUPMOC
Explanation: A step value of -1 reverses the string.
Dry Run
Program
text="PYTHON"
print(text[1:5])
| Index | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| Character | P | Y | T | H | O | N |
Output
YTHO
Real-Life Applications
- Extracting first and last names.
- Checking email domains.
- Searching keywords.
- Masking passwords.
- Generating usernames.
- Reading file extensions.
- Processing product codes.
Common Programming Mistakes
1. Assuming Stop Index is Included
text="Python"
print(text[0:3])
Output
Pyt
The character at index 3 is not included.
2. Concatenating String with Integer
age=18
print("Age="+age)
Result: TypeError
Correct
age=18
print("Age="+str(age))
3. Using an Invalid Slice
text="Python"
print(text[8:10])
Output
No error occurs; Python returns an empty string.
CBSE Important Programs
- Concatenate two strings.
- Repeat a string multiple times.
- Check whether a word exists in a string.
- Extract the first five characters.
- Extract the last four characters.
- Reverse a string using slicing.
- Print characters at alternate positions.
- Extract the file extension from a filename.
- Display only the domain name from an email address.
- Check whether a substring exists in a given string.
Viva Questions
- What is string concatenation?
- Which operator is used for concatenation?
- Which operator is used for repetition?
- What are membership operators?
- What is string slicing?
- Is the stop index included in slicing?
- How can a string be reversed using slicing?
- What does
text[:]return? - What is negative indexing?
- Give two applications of string slicing.
Exam Tips
- Remember that the stop index is excluded.
- Practice both positive and negative slicing.
- Use
::-1to reverse a string. - Use
inandnot infor membership testing. - Convert integers to strings before concatenation using
str().
Quick Revision
- + joins strings.
- * repeats strings.
inchecks whether a substring exists.not inchecks whether a substring is absent.- Slicing extracts part of a string.
- The stop index is excluded.
[::-1]reverses a string.