In this example we assign three different strings to variables animal,y and z. We can use the string function len() to find the number of characters in elephant. We then cancatenate animal with two additional strings (this is done by adding the variables). this results in the 25 character string "elephant with a big trunk". Note that blanks are counted.
# EX1.1.1
animal="elephant"
print(animal)
print(len(animal))
y=" with a"
z=" big trunk"
animal=animal+y+z
print(animal)
print(len(animal))
elephant 8 elephant with a big trunk 25
By following the string variable by brackets with a number, we address (in this example) the 0th,7th and 8th element of the string. In Python indexing variables ALWAYS begin with 0. So the first element is animal[0] or e. Blanks count, and the 8th character is indeed a blank. Negative indices are counted from the end of the string. The index -1 is the last element in the string, while -2 is the second to the last character.
# EX1.1.2
animal="elephant with a big trunk"
print(animal[0])
print(animal[7])
print(animal[8])
print(animal[-1])
print(animal[-2])
e t k n
What happened to print(animal[8]? Count the elements of the string 0,1,2,3...7,8 . The 8th element is a blank.
Here we apply the count() method using writing quo.count("a"), where quo is the string variable we defined. Note elements in the print function are separated by commas, and string are printed without the quote marks.
# EX1.1.3
quo="The Flying Dutchman is a folk tale about a ghost ship doomed to roam the seas forever, unable to return home."
print("The character 'y' appears",quo.count("y"), "times")
print("The character 'a' appears",quo.count("a"), "times")
The character 'y' appears 1 times The character 'a' appears 8 times
In the examples above, we introduced the len() function. When we wanted to find the length of a the string animal we wrote len(animal). There is another type of function which is called a method A method can only be applied to a class of objects. In this example, .count() is such a methond. We use it by writing
Note that the function applied to quo appears embedded in the print statement. Commas separate the two print strings and the function which returns the value of the count number.
The method .find("char") will return the FIRST position in the string at which it finds that character. If we were to embed this in a loop of some sort, we could get all of the locations of the character. In a slimilar fashion we can use the method index() to search for a sub-string within the string and locate the index at which it is located using .index()
# EX1.1.4
quo="The Flying Dutchman is a folk tale about a ghost ship doomed to roam the seas forever, unable to return home."
print(quo.find("u"))
print(quo.index("folk"))
12 25
Here we reassign the variable quo with the old value in which the string "seas" is replaced by "oceans".
# EX1.1.5
quo="The Flying Dutchman is a folk tale about a ghost ship doomed to roam the seas forever, unable to return home."
quo=quo.replace("seas","oceans")
print(quo)
The Flying Dutchman is a folk tale about a ghost ship doomed to roam the oceans forever, unable to return home.