Tuesday, July 26, 2022

How to determine string length?

The strings length can be determined using len built-in Python function. By using the built-in function on a string the function will return the number of characters in a string. The general form of using the len function can be written as:
string_name = "STRING"
len(string_name)
So, in the first line of code, we have created a "STRING" and assigned it to a string_name variable. In the second line of code, we have implemented the len function and the argument of that function was the string_name variable value.
Example 1 Create a string "Book" and assign it to a variable name a. Then determine the string length using the built-in len function and assign the obtained value to a variable named L. Finally, print the number of obtained string lengths.
Solution: The first step is pretty simple i.e. create a string "Book" and assign it to a variable name a.
a = "Book"
The second step is to determine the string length using the built-in len function and assign its value to the variable named L. In this case, the argument of the len function will be variable a since it contains the "Book" string whose length we are trying to determine.
L = len(a)
The third and last step is to print out the length of the a variable. This can be achieved using the print and format function.
print("L = {}".format(L))
The entire code in this example is given below.
a = "Book"
L = len(a)
print("L = {}".format(L))
The output of the previous code block is given below.
L = 4
Here it should be noted that the length of the book is 4 however the indexes of characters in the string start from 0 (you start to count from 0). In the next example, we will use the len function in for loop to print the word "Computer" in backward order.
Example 2Using for loop print the characters of a word Computer in backward order.
SolutionThe first step is to create the "Computer" string and assign it to randomly chosen strign name.
string1 = "Computer"
Now we will create for loop, however, we have to specify a range. In this case, the range will be from 0 to len(string1) which is equal to 8 with the step of 1. The body of the for loop will contain one print command that will show as output each character of a string.
for i in range(0,len(string1),1):
print("string1[len(string1)-i] = {}".format(string1[len(string1)-i]))
It is logical to assume that Python will start showing characters of a string "Computer" in backward order. However, when we run the stript we will obtain the following error.
IndexError: string index out of range
The IndexError occurred since len(string1) is 8 and in first iteration i is equal to 0. The len(string1) - i is equal to 8. The problem is that the "Computer" string has 8 characters however, indexes of this or any string in Python are counted from 0. So we wanted to show as output the character at index 8 which does not exist To solve this problem the print("string1[len(string1) - i] = {}".format(string1[len(string1) - i ]) is changed to print("string1[len(string1) - i - 1] = {}".format(string1[len(string1) - i - 1]). The value of len(string1) - i in the first iteration (8-0) will produce the IndexError since the character at index 8 does not exist. However, the value of len(string1) - i - 1 in the first iteration (8 - 0 - 1) will produce the value of 7 which is the last character in a string. When developing any part of a program it is good practice to print out every single thing to see what is going on with your code. So we have added two print functions in the for loop body. One will print the value of i variable and the second will print the value of len(string1) - i - 1.
for i in range(0,len(string1),1):
print("i = {}".format(i))
print("len(string1) - i - 1 = {}".format(len(string1) - i - 1))
print("string1[len(string1) - i - 1] = {}".format(string1[len(string1) - i - 1])
The output of the previous code is given below.
i = 0
len(string)-1-i = 7
r
i = 1
len(string)-1-i = 6
e
i = 2
len(string)-1-i = 5
t
i = 3
len(string)-1-i = 4
u
i = 4
len(string)-1-i = 3
p
i = 5
len(string)-1-i = 2
m
i = 6
len(string)-1-i = 1
o
i = 7
len(string)-1-i = 0
C
The alternative approach is to utilize the negative indices which count backward from the end of the string.
Example 3 Use negative index numbers to print out the Computer string backwardly.
Solution The first step is to save the "Computer" string to the same variable name. We will use the variable name string3.
string3 = "Computer"
The second step is to create a for loop but with a negative range from -1 up to the negative value of strings length -1 and step of -1 at each iteration. The -1 is the index of the last character in the string which is in this case "r". In the body of the for loop, we will print out the index and the character which is located at each index value.
for i in range(-1, -len(string3)-1,-1):
print("i = {}".format(i))
print("string3[{}] = {}".format(i, string3[i]))
Output:
i = -1
string3[-1] = r
i = -2
string3[-2] = e
i = -3
string3[-3] = t
i = -4
string3[-4] = u
i = -5
string3[-5] = p
i = -6
string3[-6] = m
i = -7
string3[-7] = o
i = -8
string3[-8] = C

No comments:

Post a Comment