LESSON NOTE/GUIDE
|
alpha = "abcdefghijklmnopqrstuvwxyz" s01 = alpha[3] #c s02 = alpha[25] #z s03 = alpha[-1] #z s04 = alpha[-3] #x print(s01) print(s02) print(s03) print(s04) |
We can access a specific character
in a string by using the index of that character. Indexes start at zero (just like they do
for lists). We can also use negative indexes
where -1 is the last characters. So,
-5 would refer to the 5th last character. |
PROGRAM
2 – GETTING A SUBSTRING
· Write a program that gets part of another string
based on a specified range. In
computing, part of a string is called a substring.
alpha = "abcdefghijklmnopqrstuvwxyz" s01 = alpha[1:4] #bcd s02 = alpha[10:15] #klmno s03 = alpha[7:5] #nothing print(s01) print(s02) print(s03) |
We can get a part of a string by
specifying the range we want by using the starting index and the end-before
index. For example, let’s assume we wanted
the substring cde
from the string containing the entire alphabet (like in the program above). The indexes of the characters c, d and e
are 2, 3 and 4. So, the range we
need to use is [2:5]. In regards the program above, the
string s01 gets the value bcd and the string s02 gets the
substring klmno. As for string s03, it gets no characters
because the range has a starting point that is after the ending point. |
PROGRAM
3 – MORE ON SUBSTRING
· Write a program that uses more advanced substring
ranges.
alpha = "abcdefghijklmnopqrstuvwxyz" s01 = alpha[13:-9] #nopq s02 = alpha[-20:-18] #gh s03 = alpha[10:] #klmnopqrstuvwxyz s04 = alpha[-2:] #yz s05 = alpha[:5] #abcde print(s01) print(s02) print(s03) print(s04) print(s05) |
Ranges work with the negative index
values. Of course, the first index
has to refer to a character that comes before the second index. We can also leave part of the range
blank. If we leave the number before
the colon blank, it means start at the beginning. On the other hand, if we leave the number
after the colon blank, it means that the range goes to the end of the
string. |
PROGRAM
4 – STRING METHODS RELATED TO CASE
word = input("Gimme a word!") s01 = word.capitalize() # Cap followed by lower case s02 = word.lower() # All lower case s03 = word.upper() # All upper case s04 = word.swapcase() # Reverse the case of each letter print(s01) print(s02) print(s03) print(s04) |
The example above contains four
different methods related to strings.
In each case, when we call the method to do a transformation on the
string, the method returns an entirely new string. The original string always remains
unchanged. The effect of each method is given
in the comments above. But you are
encouraged to try this out and explore! |
PROGRAM
5 – FIND AND COUNT
w = input("Can I have a word
please?") tot1 = w.count("b") # number of times 'b' is in w tot2 = w.count("B") # number of times 'B' is in w loc = w.find("a") # index of first 'a' in w print(tot1) print(tot2) print(loc) |
The above methods all calculate a
value based on the content of the string w and the argument that is passed
to them. The method count looks at the
string w and counts the amount of times that the provided string (b or B in
the statements above) appear in the string. The method find looks at the string
w and returns the index of the provided string if it appears inside w. If it doesn’t then it return -1. |
PROGRAM
6 – THE FUNCTIONS ORD & CHR
print(ord("a")) # 97 print(chr(97)) # a print(ord("A")) # 65 print(ord("Z")) # 90 print(ord("a")) # 97 print(ord("z")) # 122 print(chr(120)) # x print(chr(88)) # X |
The ord
function gives us the ASCII number for a character. The chr
function does the opposite, it gives us the ASCII character for a provided
number. Note that the ord
function will cause an error if it is provided with a string that contains
more than one character. Got time? De a web search for “ASCII chart” to see
a full listing of ASCII characters and their corresponding numbers. |
PROGRAM
7 – COMBINING ORD & CHR
print(ord("m")) # 109 print(chr(ord("m"))) # m print(ord(chr(109))) # 109 val = ord("m") + 1 print(chr(val)) # n (the next letter after m) |
The ord and
chr functions can be used together to manipulate
characters. Notice in lines 2 and 3
of the code, both functions are used to cancel each other out. In the last two lines, we use the
functions to convert the letter m into the next letter, the letter n. This is often referred to as shifting. |
PROGRAM
8 – SHIFTING CHARACTER BY 1
letter = input("Enter a
character") value = ord(letter) + 1 print(chr(value)) THE
CODE – OPTION 2 letter = input("Enter a
character") print(chr(ord(letter)
+ 1)) |
To get the next character of a
character, we simply find that character’s ASCII value, then add one, then
convert that number back to ASCII. |
PROGRAM
9 – BUILDING A STRING IN A LOOP
word = "" for x in range(1, 6): word = word + input("Enter
a letter:") print(word) |
In the first line, we create a
string object named word. We set it
to the empty string. The for loop is set to loop 5
times. Inside the loop, for each pass, we
append the inputted letter to the string word. Finally, we print the created word
at the end. |
PROGRAM
10 – SHIFTING ENTIRE WORD BY 1
word = input("Enter a
word") word2 = "" for c in word: word2 = word2 + chr(ord(c) + 1) print(word2) THE
CODE – OPTION 2 word = input("Enter a
word") for c in word: print(chr(ord(c) + 1), end="") # no next line |
Note that this program is
considerably more complex than other programs in this lesson because we are
gradually building a new string each pass in the loop. In the first line of code, the user
provides a word. In the second line of code, we
create the string object named word2 and set it to the empty string. The for loop goes through the
string word with c getting each character in word one at a time for each
loop pass. In each pass, we append the shifted
letter at the end of word2. Finally, after the loop, we output
word2.
This program is considerably easier
to understand than option 1.
However, option 1 does allow for more flexibility and could be used
in more scenarios. The only new concepts in this
program is the use of end=”” in the print statement. This forces the cursor to remain on the
same line instead of moving to the next line. |