Before session 5
1. Strings
Watch the videos:
2. Test
Go to Moodle and take the fifth test on video lectures.
3. Examples
Strings are sequences of characters. They are used in a wide variety of applications, and the range of application is increasing very fast.
Example 1. Estonian Personal Identifications Code
In Estonia, a personal identification number (Estonian: isikukood) is defined as a number formed primarily on the basis of the gender and the date of birth of a person. The EPIN of each citizen is unique, therefore it is possible to identify a person by one's EPIN. The national ID-card, its associated certificates and digital signatures use the PINs.
An EPIN consists of 11 digits, generally given without any whitespace or other delimiters. The form is GYYMMDDSSSC, where G shows the gender and the century of one's birth (odd numbers for male, even numbers for female: 1-2 for male and female born in the 19th century, respectively; 3-4 for male and female born in the 20th century; 5-6 for male and female born in the 21st century), SSS is a serial number uniquely identifying people born on the same date, and C is the checksum.
The following program prompts the user for an EPIN, looks for one's birthday date from the entered code and prints the date out in the format dd.mm.yyyy:
code = input("Please enter a personal identification number: ") #example code: 48007140350 date = code[5] + code[6] month = code[3] + code[4] if code[0] == "1" or code[0] == "2": century = "18" elif code[0] == "3" or code[0] == "4": century = "19" else: century = "20" year = century + code[1] + code[2] print("The birthday of the person with the code", code, "is", date+"."+month+"."+year)
Example 2. Room number from address
The following program prompts the user for the address. The program looks for and prints out the room number from the entered address (assume that the address is entered in the form of street_name building_number-room_number, town_name; e.g. Liivi 2-203, Tartu).
address = input("Please enter an address: ") fpos = address.find("-") lpos = address.find(",", fpos) room = address[fpos+1:lpos] print("Room number is", room)
4. Solve and submit
Exercise 1. Name and grades
Write a program that asks the user for his first name and grades. Regardless of how the user enters the name and grades (in lower and capital letters or mixed), the output of the program has to be formatted so that the name begins with the capital letter followed by lower case letters, and all the grades are in the upper case. On the next line, the program has to show the number of the grades. On the following line, the grade for the second course has to be shown (assume that the number of the entered grades is at least 2). On the last line, the program has to show the number of A's and B's (counted together).
Here is an example of program output:
Enter first name: mAriNa Enter grades: aaAbBcDaD Hello Marina, your grades are AAABBCDAD You have 9 grades Your grade for the second course is A The number of A's and B's is 6
Exercise 2. Username from url
Every user of the University of Tartu (a student or a member of staff) has his/her home directory in the Study Information System. The user can create a webpage on the university server. The url for his/her webpage is www.ut.ee/~username/ (e.g. www.ut.ee/~vilo/). Write a program that prompts the user for a university url. The program has to look for and print out the user name.
Here is an example of the program output:
Please enter url: http://www.ut.ee/~koit/KT/index_eng.html Username is koit
Go to Moodle and submit your solutions under Homework 5.