Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Text is the primary medium through which humans interact with software. In Python, textual data is represented using the str (string) data type. A Python string is an ordered, immutable sequence of Unicode characters.
Unlike older languages like C where strings are raw null-terminated arrays of 8-bit ASCII bytes, Python 3 treats strings as native Unicode objects. Whether your application handles English, Hindi, Arabic, Japanese, or modern emoji symbols, Python handles every character seamlessly without external encoding libraries.
+-------------------------------------------------------------------------+
| STRING IMMUTABILITY REAL-WORLD ANALOGY |
+-------------------------------------------------------------------------+
1. THE CHALKBOARD (Mutable - like a Python list):
- You write a word with chalk on a blackboard.
- You can erase the 3rd letter with your finger and write a new one.
- The physical chalkboard remains identical.
2. THE MARBLE PLAQUE (Immutable - a Python String):
- The foundation stone of a university building carved into granite.
- You CANNOT erase the 4th letter without shattering the stone!
- If you want to change the text, you must carve an ENTIRELY NEW plaque!
- In Python: greeting = "Hello"; greeting[0] = "J" -> CRASH (TypeError)!
- You must create a new string: greeting = "J" + greeting[1:]
+-------------------------------------------------------------------------+In CPython, strings are stored as compact Unicode objects. When you create or reassign a string, Python allocates a new memory block:
Python allows you to define string literals using single ('), double ("), or triple (''' or """) quotes:
Python 3 strings are native Unicode (UTF-8). You can store and manipulate diverse languages without encoding errors:
Once a string is instantiated, you cannot alter, replace, or delete any of its individual characters:
in / not in)To determine the number of characters in a string, pass it to the built-in len() function. To check if a substring exists within a string, use the intuitive in and not in membership operators:
| Scenario | Anti-Pattern (Don't) | Best Practice (Do) | Why |
|---|---|---|---|
| Quotes with Apostrophes | 'Don\'t do this' | "Don't do this" | Using double quotes eliminates distracting backslash escapes. |
| Multiline Paragraphs | "Line 1\nLine 2\nLine 3" | Triple quotes """Line 1...""" | Triple quotes preserve visual layout and make long text maintainable. |
| Substring Search | if text.find("key") != -1: | if "key" in text: | The in keyword is cleaner, faster, and idiomatic Python. |
| Modifying Strings | Attempting s[0] = 'a' | Slice & stitch: 'a' + s[1:] | Strings are strictly immutable in Python. |
+-------------------------------------------------------------------------+ | PYTHON STRINGS CHEAT SHEET | +-------------------------------------------------------------------------+ - Definition: Ordered, immutable sequence of Unicode characters - Quotes: 'single', "double", '''triple''', """multiline""" - Unicode: Full support for Hindi, regional scripts, and emojis - Immutability: s[0] = 'X' raises TypeError! Strings cannot be mutated - Character Count:len(s) returns total characters including spaces - Membership: 'word' in text returns True if substring exists +-------------------------------------------------------------------------+
Python strings are immutable. You cannot assign directly to a character index:
In Python slicing text[start:stop], the stop index is exclusive (not included in the slice):
msg = "Hello"; msg[0] = "Y" in Python?A. msg successfully updates to "Yello" B. Python raises a TypeError: 'str' object does not support item assignment C. A new variable Y is created D. The first letter is deleted
A. Single quotes '...' B. Double quotes "..." C. Triple quotes """...""" or '''...''' D. Backticks ` ... `
len("Code 101")?A. 7 B. 8 C. 6 D. 9
"data" in "Database Management"?A. True B. False C. TypeError D. None
A. ASCII only B. Latin-1 only C. Unicode (UTF-8 / compact representation) D. EBCDIC
Write a complete Python program that accepts and validates a student registration card. The script validates that the student name, Aadhaar card number, and regional greetings conform to length and format rules using string literals, len(), and membership tests.
In the next lesson, we will continue your Python learning journey with Slicing & Indexing (5: Strings).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Math Module (math, random) | Slicing & Indexing |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.