TN Online TestSamacheer Kalvi practice

Strings and String manipulation - Study Notes

Share this chapter: Telegram

Chapter Summary

This chapter provides a detailed exploration of strings in Python, emphasizing their characteristics as sequence data types representing arrays of Unicode characters. It covers the essential rules of string creation using single, double, and triple quotes, and explains their fundamental immutable property. Students will learn how to navigate and extract portions of strings using positive and negative subscripts, apply diverse string operators, and utilize built-in functions to format, inspect, and manipulate textual data efficiently.

Learning Objectives

Key Concepts and Definitions

String Immutability

A key property of Python strings is that they are immutable. Once a string is declared and allocated in memory, its individual characters cannot be changed or deleted. Attempting to assign a new character to a specific index results in a runtime error. To modify a string, a new string must be constructed and assigned back to the variable.

Positive and Negative Subscripts

Python assigns two indices to every character in a string to facilitate access. Positive subscripts start at 0 from the left-hand side and go up to length minus one. Negative subscripts start at -1 from the rightmost character and count backwards to the negative of the string length.

Slicing and Stride

Slicing is the process of extracting a substring from a main string. The slicing operator uses a start and stop index, retrieving characters up to but not including the stop index. A third parameter, the stride, dictates the step size when moving through the string. A negative stride can be used to traverse and print the string in reverse order.

Membership Operators

The operators in and not in are used to verify if a specific substring or character is present within another string, returning a Boolean value of True or False.

Worked Methods

Reversing a String Without Stride

While a stride of -1 easily reverses a string, reversing can also be achieved using a loop. By iterating through each character of the input string and prepending it to a new string accumulator, the string is successfully reversed.

Formatting Output with format()

The format function allows dynamic replacement of placeholders represented by curly braces. For instance, combining values into a single sentence can be elegantly done by passing arguments to the format method, which sequentially inserts them into the placeholders.

Common Exam Traps

Exam Tips

Solved MCQs → Practice test →

More for this chapter

Book Back Questions10 textbook MCQs · solved Additional MCQs15 extra MCQs · solved Practice TestInteractive · instant score Book Back TestTest yourself on the textbook set Additional MCQ TestTest yourself on the extra set Formula SheetAll key formulas

More chapters in Computer Science

View all
1 Function 2 Data Abstraction 3 Scoping 4 Algorithmic Strategies 5 Python -Variables and Operators 6 Control Structures 7 Python functions 9 Lists, Tuples, Sets and Dictionary 10 Python Classes and objects 11 Database Concepts 12 Structured Query Language (SQL) 13 Python and CSV files 14 Importing C++ programs in Python. 15 Data manipulation through SQL 16 Data visualization using pyplot: line chart, pie chart and bar chart