Before session 5
Strings
Watch the videos. Next two videos (part 1 and part 2) show how Python stores and manipulates textual data using string variables and functions.
Slides
Text-book: chapter 6 - strings
Test
Go to Moodle and take the fifth test on the video lectures.
Homework
The deadline for homework is Sunday. Nevertheless, you can start with homework already before the session.
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 sex and date of birth of a person. The EPIN of each citizen is unique, therefore it is possible to identify the 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 sex and the century of 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 his birthday date from the entered code and prints it 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 an 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)
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. Next, 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 the 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
Hint: Look at string functions from string library.
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 then has to look for the user name and print it out.
Here is an example of the program output:
Please enter url: http://www.ut.ee/~koit/KT/index_eng.html Username is koit