Python String Mastery
String Basics
Strings Are Unchangeable
In Python, once a string is created, it can't be modified. This property is called immutability. Think of a string like a message written in permanent ink. You can't just erase one letter and write a new one in its place. If you want to change the message, you have to write a whole new one.
Let's say you have a string and want to change a character. Python won't allow it and will raise an error.
# This will cause an error!
word = "Python"
word[0] = "J" # Trying to change 'P' to 'J'
Running this code results in a TypeError, because you cannot change an item within a string. To make a change, you must create a new string. For example, you could create "Jython" by combining "J" with a piece of the original string.
word = "Python"
new_word = "J" + word[1:]
print(new_word)
# Output: Jython
This doesn't change the original word string. It creates an entirely new string, new_word, and assigns it the new value.
Accessing Characters by Index
So, how do we grab parts of a string, like we did with word[1:]? We use indexing. Each character in a string has a position, or index. Think of it like street addresses for each letter. Importantly, Python starts counting from 0, not 1. The first character is at index 0, the second is at index 1, and so on.
To get a character, you use square brackets [] with the index inside.
word = "PYTHON"
print(word[0]) # First character
print(word[3]) # Fourth character
P H
Python also supports negative indexing, which is useful for accessing characters from the end of the string. The index -1 refers to the last character, -2 to the second-to-last, and so on.
word = "PYTHON"
print(word[-1]) # Last character
print(word[-6]) # First character
N P
Slicing Strings
If you want to get more than one character, you can slice the string. Slicing lets you grab a substring. The syntax is start:stop, where start is the index of the first character you want, and stop is the index of the character after the last one you want. This might seem odd, but it means that stop - start is the length of the sliced string.
word = "PYTHON"
# Get characters from index 2 up to (but not including) index 5
print(word[2:5])
THO
You can leave out the start or stop index. If you omit the start, the slice begins from the start of the string. If you omit the stop, it goes all the way to the end.
word = "PYTHON"
# From the beginning up to index 2 (exclusive)
print(word[:2])
# From index 2 to the end
print(word[2:])
# A copy of the whole string
print(word[:])
PY THON PYTHON
There's also a third parameter called step, which lets you skip characters. The format is start:stop:step. For example, a step of 2 will take every second character.
word = "PYTHON"
# Get every other character from the whole string
print(word[::2])
# A common trick to reverse a string!
print(word[::-1])
PTO NOHTYP
What does it mean for a string to be "immutable" in Python?
What is the result of running the following Python code?
word = "Python"
word[0] = "J"
print(word)
Indexing and slicing are fundamental tools for working with text data in Python.