How to Change the Character List in Python
In the Python programming language, text can be stored as a string and as a character list. A string is a single object that represents a length of text. A character list is a list of many objects, each of which represent a single character. The advantage of a character list over a string is that you can access a character by its index. This allows you to selectively change characters by their location in the list.
Instructions
-
-
1
Open the IDLE text editor that comes with the Python download. The IDLE text editor is found in Program Files (or Applications for Macintosh), in the Python directory. A blank source code file opens in the IDLE text editor window.
-
2
Declare a list of characters by writing the following at the top of the source code file:
textList = ['C', 'a', 't']
-
-
3
Print out the list of characters using the print() function, as shown below:
print(textList)
-
4
Change the first character in the list by accessing it by its index. Lists start at index 0, so the first letter is actually the 0th index. The syntax for changing the first letter in the list looks like this:
textList[0] = 'D'
-
5
Change the second letter in the list like this:
textList[1] = 'o'
-
6
Change the third letter in the list like this:
textList[2] = 'g'
-
7
Print out the modified list using the print() function:
print(textList)
-
8
Execute the program by pressing the "F5" key. The output looks like this:
['C', 'a', 't']
['D', 'o', 'g']
-
1