Tuesday, July 26, 2022

What is a string?

A string is a sequence of characters and you can access each character of a string using the bracket notation. If the string is assigned to a variable then to access the character of the string type the variable name is followed by square brackets and inside squared brackets is the index number. In the following example, we are trying to access the character with index number 0.
string_name = "Python"
character = string_name[0]
In first code line the string is assigned to a variable name string_name. Then in the second line of code we have assigned the string_name[0] to variable name character. The string_name is the variable name to which the string "Python" was assigned. The number in the brackets is called the index and it indicates which character in the sequence we want to access. In this example, the index is 0 which is the character "P". In the following table, the indexes of all characters that make "Python" is shown.
P y t h o n
0 1 2 3 4 5

Example 1 Assign the string "Computer" to variable name String. Extract the character at index 2 and print out the character.
Solution: To create string "Computer" and assign it to variable name String type in the following code.
String = "Computer"
To extract the character of a "String" at index 2 and assign it to the variable name "Character" type in the following code.
Character = String[2]
To show the value of the "Character" variable use the print function.
print("Character = {}".format(Character))
The entire code of this example as well as the output is given below.
string = "Computer"
Character = String[2]
print("Character = {}".format(Character))
Output:
m
Logically the character at number 2 would be "o" not "m". However, in Python, the index starts at 0 i.e. an offset from the beginning of the string. So the first letter at index 0 is "C". To select and print out the first letter we have to type the following.
print("String[0] = {}".format(String[0]))
And the output to the previous code line would be:
"C"
In the following table, the index numbers of all the characters that make the word "Computer" are shown.
C o m p u t e r
0 1 2 3 4 5 6 7

Example 2 Write a program that will print each character of the string "Computer" using a while loop. At each iteration print only one character.
Solution: Before we create a while loop we have to define "Computer" string and assign it to variable name "string". Then you have to define variable i and assign the value of 0 to this variable. At each iteration, the variable i will increment i.e. be updated by the value of 1. The main usage of this variable is to use it as the index in the bracket notation for extracting characters from the string.
string = "Computer"
i = 0
Now we can create a while loop which will contain the print function for printing each character, updating a value of the i variable and the break condition to break the execution after all characters are printed. Since the last character in string "Computer" is "r" if string[i] == "r" then the while loop will end.
while True:
print(strint[i])
i = i + 1
if string[i] == "r":
break
After all, characters are printed the execution of the while loop will be terminated. Finally, create output that is a string "The End".
print("The end")
After running the code from Example 2 the following output is obtained.
C
o
m
p
u
t
e
r
The end.

No comments:

Post a Comment