Computer Science

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:

  • in
  • not 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

  1. Concatenate two strings.
  2. Repeat a string multiple times.
  3. Check whether a word exists in a string.
  4. Extract the first five characters.
  5. Extract the last four characters.
  6. Reverse a string using slicing.
  7. Print characters at alternate positions.
  8. Extract the file extension from a filename.
  9. Display only the domain name from an email address.
  10. Check whether a substring exists in a given string.

Viva Questions

  1. What is string concatenation?
  2. Which operator is used for concatenation?
  3. Which operator is used for repetition?
  4. What are membership operators?
  5. What is string slicing?
  6. Is the stop index included in slicing?
  7. How can a string be reversed using slicing?
  8. What does text[:] return?
  9. What is negative indexing?
  10. Give two applications of string slicing.

Exam Tips

  • Remember that the stop index is excluded.
  • Practice both positive and negative slicing.
  • Use ::-1 to reverse a string.
  • Use in and not in for membership testing.
  • Convert integers to strings before concatenation using str().

Quick Revision

  • + joins strings.
  • * repeats strings.
  • in checks whether a substring exists.
  • not in checks whether a substring is absent.
  • Slicing extracts part of a string.
  • The stop index is excluded.
  • [::-1] reverses a string.