Python: a string application

The tutor shows an example program centered around string indexing.

In my July 31 post, I opened the topic of string indexing in Python. A string, moving forward, starts at position [0]; however, from the end, it starts at [-1].

The question could be asked, for a given string, the place where, read from the front, it coincides with the read from the back:


newstring=”Vancouver”
for i in range(len(newstring)):
   if newstring[i]==newstring[-(i+1)]:
      print(“Coincidence index: ” +str(i))
      print(“Coincidence character: “+newstring[i])

produces the output

Coincidence index: 4
Coincidence character: o

String indexing can be very useful in certain applications:)

Source:

Donaldson, Toby. Python, 3rd Ed. Peachpit Press, 2014.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Leave a Reply