2024 25 COL100 Lab 8 Strings
2024 25 COL100 Lab 8 Strings
Lab 8: Strings
February 28, 2025
1 Strings
Strings in Python are used to represent characters and can be defined using single (’) or
double (”) quotes.
Example
The literal ‘123’ represents a string of three characters, not the number 123.
Examples
1 # Concatenation
2 result = 'a ' + 'a ' # Output : ' aa '
3
4 # Repetition
5 repeated = 3 * 'a ' # Output : ' aaa '
Example
Example
Indexing Errors
Attempting to access an index out of range will raise an IndexError.
1 # This will raise an IndexError
2 out_of_range = s [3] # Output : IndexError : string index out of range
1.4 Slicing
Slicing allows you to extract substrings from a string.
Example
If the start index is omitted, it defaults to 0. If the end index is omitted, it defaults
to the length of the string.
1 full_string = s [:] # Output : ' abc '
Non-contiguous Slicing
You can also slice with a third argument to skip elements.
1 sliced = ' 123456789 ' [0:8:2] # Output : '1357 '
Page 2
1.5 Type Conversions
Python provides built-in functions to convert types, such as str(), int(), and float().
1 num = 30000000
2 fraction = 1 / 2
3 print ( num * fraction , ' is ' , fraction * 100 , '% ' , ' of ' , num )
4 # Output : 15000000.0 is 50.0 % of 30000000
Modifiers
You can include modifiers in f-strings to control the formatting.
1 print ( f '{ num * fraction : ,.0 f } is { fraction * 100}% of { num : ,} ')
2 # Output : 15 ,000 ,000 is 50.0% of 30 ,000 ,000
Page 3
2 Submission Problem
2.1 Palindrome Checker Problem
Problem Statement
You are given a single string s. Your task is to write a program to determine if the string
s is a palindrome. A palindrome is a string that reads the same backward as forward,
ignoring spaces, punctuation, and case differences.
2.1.1 Instructions
• The first line contains the string s, which may include letters, numbers, spaces, and
punctuation.
Input
Output
1 True
Explanation: If we ignore spaces, punctuation, and case, the string reads the same
forward and backward.
Input
1 race a car
Output
1 False
Explanation: Even after ignoring spaces and punctuation, the string does not read
the same forward and backward.
Page 4
Constraints
• The string s will contain only ASCII characters.
• Treat uppercase and lowercase letters as the same for palindrome checking.
Page 5
3 Practice Problem
3.1 Finding the Last Position of a Substring
Problem Statement
Given a string s and a substring sub, write a program to find the last occurrence (position)
of sub within s. If the substring does not exist in the string, return -1.
Input
1 hellohello
2 lo
Output
1 8
Explanation: The substring ”lo” occurs twice in the string ”hellohello”. The last
occurrence starts at index 8.
Input
1 openai
2 ai
Output
1 4
Explanation: The substring ”ai” occurs only once in ”openai”, starting at index 4.
Input
1 testcase
2 xyz
Page 6
Output
1 −1
Explanation: The substring ”xyz” does not appear in ”testcase”, so the output is -1.
Constraints
• The length of the main string s will be between 1 and 105 .
• The length of the substring sub will be between 1 and 104 .
Input
1 H e l l o , World !
Output
1 ,: 1
2 !: 1
3 d: 1
4 e: 1
5 h: 1
6 l: 3
7 o: 2
8 r: 1
9 w: 1
Explanation: In the string "Hello, World!", the program counts each character’s
occurrences, ignoring case and spaces, and outputs the frequency of each character in
alphabetical order.
Page 7
Input
1 Data S c i e n c e
Output
1 a: 2
2 c: 2
3 d: 1
4 e: 2
5 i: 1
6 n: 1
7 s: 1
8 t: 1
Explanation: In the string "Data Science", each character frequency is counted and
displayed in alphabetical order.
• The first line contains two integers: the number of coin denominations n and the
target amount amount.
• The second line contains n integers representing the available coin denominations.
• Output the minimum number of coins needed to make up the amount, or -1 if it is
not possible.
Input
1 3 11
2 1 2 5
Output
1 3
Explanation: To achieve a target amount of 11, we can use three coins: two 5-coin
denominations and one 1-coin denomination (5 + 5 + 1 = 11).
Page 8
Input
1 2 3
2 2 4
Output
1 −1
Explanation: It is impossible to achieve the target amount of 3 with only coins of
denominations 2 and 4.
Constraints
• 1 ≤ n ≤ 100
• 1 ≤ amount ≤ 104
• Each coin denomination will be a positive integer not greater than 104 .
Page 9