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.
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. Narva 18-2006, Tartu).
address = input("Please enter an address: ") fpos = address.find("-") lpos = address.find(",",fpos) room = address[fpos+1:lpos] print("Room number is", room)
Homework
The deadline for homework is Thursday. You should start with homework already before the session.
Homework 5.1 Chess piece moves
In chess notation short piece names are used, for example, K = king, Q = queen, N = knight etc. and each move of a piece is indicated by the piece's uppercase letter, plus the coordinate of the destination square in lowercase. For example, Be5 (bishop moves to e5), Nf3 (knight moves to f3). For pawn moves, a letter indicating pawn is not used, only the destination square is given. For example, c5 (pawn moves to c5).
Write a function named chessMoves
that finds the number of moves made in a chess game with a specific chess piece (excluding pawn). The function should take 2 strings as arguments, the first one is all the chess moves separated by a space character and the second one is the piece for which the number of moves will be returned. The function should find the number of moves made with the selected piece and return it. The program should print out the selected piece and how many moves were made with it. The piece for function argument should be received through user input.
Example function call:
>>>chessMoves("e4 e5 Qh5 Nc6 Bc4 Nf6 Qf7", "N") 2
Example of the program output:
>>> %Run solution.py
Please select a chess piece: N
There were 2 moves made with N.
If you have been struggling with this exercise for some time, maybe you can get help from troubleshooter: https://progtugi.cs.ut.ee#/ts/623ade52ef7eaf5634394a42/
. It tries to explain the most common problems and give hints.
Homework 5.2 String correction
A cat ran over your keyboard and your string got messed up. Luckily it only ran over all numerical keys. Write a program that removes all the numbers from the string and capitalizes the first letter of each word and returns the result.
Hint: You can use isnumeric()
to check if a character is a number or not and title()
to capitalize every word.
Example:
>>> %Run solution.py
13H3e1llo0 312w20orl3d!432
Hello World!
If you have been struggling with this exercise for some time, maybe you can get help from troubleshooter: https://progtugi.cs.ut.ee#/ts/623ae19def7eaf5634394a71/
. It tries to explain the most common problems and give hints.
Go to Moodle and upload your solution into Homework 5.