- Syntax
- Example 1: Reading a Character
- Example 2: Attempting to Modify a Character
- Example 3: Creating a New String
- Example 4: Using Concatenation to Create a New String
- Example 5: Original String Remains Unchanged
- Example 6: Replacing a Character Using Slicing
- Example 7: User Input
- Example 8: Checking Object Identity
- Example 9: Strings Returned by Methods
- Example 10: Verifying Immutability
- Common Mistakes
- Best Practices
- Key Points to Remember
A string in Python is immutable, which means its contents cannot be changed after the string has been created.
Once a string is assigned to a variable, individual characters cannot be modified, removed, or replaced directly.
If you want to change a string, Python creates a new string instead of modifying the existing one.
String immutability makes strings safer to use, allows Python to optimize memory usage, and prevents accidental modification of text.
Syntax #
string_name[index]
You can read characters using indexing, but you cannot modify them.
Example 1: Reading a Character #
main.py
language = "Python"
print(language[0])
Output #
P
Explanation #
- Indexing can be used to read characters.
- The character at index
0isP. - Reading characters does not modify the string.
Example 2: Attempting to Modify a Character #
main.py
language = "Python"
language[0] = "J"
Output #
TypeError: 'str' object does not support item assignment
Explanation #
- Python does not allow characters in a string to be changed.
- Strings are immutable.
- Attempting to assign a new character produces a
TypeError.
Example 3: Creating a New String #
main.py
language = "Python"
language = "Jython"
print(language)
Output #
Jython
Explanation #
- The original string is not modified.
- Instead, the variable is assigned a completely new string.
- The variable now refers to the new string.
Example 4: Using Concatenation to Create a New String #
main.py
word = "Python"
new_word = "J" + word[1:]
print(new_word)
Output #
Jython
Explanation #
word[1:]returns"ython"."J"is concatenated with"ython".- A new string
"Jython"is created. - The original string remains
"Python".
Example 5: Original String Remains Unchanged #
main.py
text = "Python"
copy = text
copy = "Java"
print(text)
print(copy)
Output #
Python
Java
Explanation #
- Initially, both variables refer to the same string.
- Reassigning
copydoes not affecttext. - The original string remains unchanged.
Example 6: Replacing a Character Using Slicing #
main.py
word = "Python"
new_word = word[:1] + "A" + word[2:]
print(new_word)
Output #
PAthon
Explanation #
word[:1]returns"P".word[2:]returns"thon".- Concatenating the parts creates a new string.
- The original string is not modified.
Example 7: User Input #
main.py
name = input("Enter your name: ")
name = name.upper()
print(name)
Sample Input #
alice
Output #
ALICE
Explanation #
upper()returns a new uppercase string.- The original input string is not modified.
- The variable
nameis updated to reference the new string.
Example 8: Checking Object Identity #
main.py
text = "Python"
print(id(text))
text = text + " Programming"
print(id(text))
Output #
140437761...
140437845...
Explanation #
id()returns the memory identity of an object.- After concatenation, Python creates a new string.
- The memory identity changes because a new object is created.
Note: The exact numbers returned by
id()vary from one execution to another.
Example 9: Strings Returned by Methods #
main.py
text = "python"
uppercase = text.upper()
print(text)
print(uppercase)
Output #
python
PYTHON
Explanation #
upper()does not modify the original string.- It returns a new uppercase string.
- The original string remains unchanged.
Example 10: Verifying Immutability #
main.py
word = "Computer"
print(word)
new_word = word.replace("C", "L")
print(new_word)
print(word)
Output #
Computer
Lomputer
Computer
Explanation #
replace()creates a new string.- The original string is not modified.
- Both strings exist independently.
Common Mistakes #
1. Trying to Modify a Character #
Incorrect
word = "Python"
word[1] = "A"
Output #
TypeError: 'str' object does not support item assignment
Reason
Strings are immutable.
Individual characters cannot be changed.
2. Assuming String Methods Modify the Original String #
Incorrect
text = "python"
text.upper()
print(text)
Output #
python
Reason
upper() returns a new string.
It does not modify the original one.
Correct
text = text.upper()
print(text)
3. Assuming Concatenation Changes the Original String #
Incorrect
text = "Hello"
text + " World"
print(text)
Output #
Hello
Reason
Concatenation creates a new string.
The original string remains unchanged.
Correct
text = text + " World"
print(text)
4. Confusing Strings with Lists #
Incorrect
text = "Python"
text[2] = "X"
Output #
TypeError: 'str' object does not support item assignment
Reason
Lists are mutable, but strings are immutable.
Best Practices #
- Treat strings as read-only objects.
- Use slicing and concatenation to create modified versions of strings.
- Store the result returned by string methods.
- Remember that every modification produces a new string.
- Avoid assuming that string methods modify the original string.
Key Points to Remember #
- Strings in Python are immutable.
- Individual characters cannot be modified after a string is created.
- String methods return new strings instead of modifying existing ones.
- Concatenation creates a new string.
- Slicing creates a new string.
- Reassigning a variable changes what it refers to, not the original string.
- Attempting to modify a character directly raises a
TypeError.