WEEK-4
Pre-lab Task:
1. What is string in python?
Ans: In Python, a string is a sequence of characters enclosed in either single quotes (') or double quotes
("). Strings are used to represent text data.
For example:
my_string = "Hello, World!"
2. Predict the output of the following code?
str = “Hello”
print(str[:3])
Ans: Hel
3. How do you create a string in Python?
Ans: In Python, you can create a string by enclosing a sequence of characters in either single quotes (') or
double quotes (").
For example:
my_string1 = 'Hello, World!'
(or)
my_string2 = "Hello, World!"
4. What methods can you use to convert a string to uppercase and lowercase?
1. Convert to Uppercase: upper() method:
my_string = "Hello, World!"
upper_string = my_string.upper()
print(upper_string)
Output:
HELLO, WORLD!
2. Convert to Lowercase: lower() method:
my_string = "Hello, World!"
lower_string = my_string.lower()
print(lower_string)
Output:
hello, world!
5. How do you compare two strings in Python?
Ans: In Python, you can compare two strings using comparison operators. The common comparison
operators are:
1. Equality (==): Checks if two strings are exactly the same.
2. Inequality (!=): Checks if two strings are different.
3. Greater than (>), Less than (<) : Compares strings based on lexicographical order.
4. Greater than or equal to (>=), Less than or equal to (<=) : Compares strings based on
lexicographical order.
In-Lab Task:
1. Write a program which does the following
Declare a string variable x
Accept a text user input - the name of the user - and store it in the variable x
Output and print to the console Hello before the user defined
name. Remember to add a space between Hello and x
Sample Input:
Chef
Sample Output:
Hello Chef
Link: https://www.codechef.com/learn/course/python/LTCPY10/problems/PYTHCL55
Solution:
x = input()
print("Hello " + x)
2. Given a string of odd length greater 5, return a string made of the middle three chars of a given String.
Sample Input:
"JhonDipPeta"
Sample Output:
Dip
Solution:
input_str = "JhonDipPeta"
middle_index = len(input_str) // 2
middle_three_chars = input_str[middle_index - 1 : middle_index + 2]
print(middle_three_chars)
3. Write a python program that replaces all vowels in a given string with a specified character.
Sample Input:
"PYTHON CODING" and '*'
Sample Output:
"PYTH*N C*D*NG"
Solution:
input_str = "PYTHON CODING"
replacement_char = '*'
vowels = "AEIOUaeiou"
result_str = ""
for char in input_str:
if char in vowels:
result_str += replacement_char
else:
result_str += char
print(result_str)
Post-Lab Task:
1. Arrange string characters such that lowercase letters should come first. Given an input string
with the combination of the lower and upper case arrange characters in such a way that
lowercase letters should come first.
Sample Input:
KLUniversity
Sample Output:
niversityKLU
Solution:
input_str = "KLUniversity"
lowercase_chars = ""
uppercase_chars = ""
for char in input_str:
if char.islower():
lowercase_chars += char
else:
uppercase_chars += char
result_str = lowercase_chars + uppercase_chars
print(result_str)
2. Consider a Python program that takes a string and returns `True` if the string is a
palindrome, otherwise `False`.
Sample Input 1:
"RADAR"
Sample Output 1:
True
Sample Input 2:
"RAMA"
Sample Output 2:
False
Solution:
input_str1 = "RADAR"
input_str2 = "RAMA"
input_str1 = input_str1.upper()
input_str2 = input_str2.upper()
is_palindrome1 = input_str1 == input_str1[::-1]
is_palindrome2 = input_str2 == input_str2[::-1]
print(is_palindrome1)
print(is_palindrome2)
3. A python program that takes a string and a delimiter, splits the string by the delimiter, and then joins
itback with a hyphen `-`.
Sample Input:
str = "CSE, CSIT, ECE, AIDS”
delimiter = "-"
Sample Output:
"CSE-CSIT-ECE-AIDS"
Solution:
input_str = "CSE, CSIT, ECE, AIDS"
delimiter = ", "
split_str = input_str.split(delimiter)
joined_str = '-'.join(split_str)
print(joined_str)
Skill Session:
1. Write a program that accepts four words from the user in a single line and prints
them in reverse order.
Sample Input:
Air Water Earth Fire
Sample Output:
Fire Earth Water Air
Link: https://www.codechef.com/learn/course/python/LTCPY10/problems/PYTHCL50C
Solution:
input_str = input()
words = input_str.split()
reversed_words = words[::-1]
output_str = ' '.join(reversed_words)
print(output_str)
2. Implement a Python program that takes two strings and returns `True` if they are anagrams of each
other, otherwise `False`.
Sample input 1: STUDY
DUSTY
Sample output 1: True
Sample input 2: STUDY
TUSDY
Sample Output 2: False
Solution:
str1 = "STUDY"
str2 = "DUSTY"
str1 = str1.upper()
str2 = str2.upper()
are_anagrams = sorted(str1) == sorted(str2)
print(are_anagrams)
3. Design a python program that takes a string and returns a compressed version where consecutive
duplicates of characters are replaced with the character followed by the count.
Sample input: aaabbc
Sample Output: a3b2c1
Solution:
input_str = "aaabbc"
compressed_str = ""
count = 1
for i in range(1, len(input_str)):
if input_str[i] == input_str[i - 1]:
count += 1
else:
compressed_str += input_str[i - 1] + str(count)
count = 1 # Reset count for the new character
compressed_str += input_str[-1] + str(count)
print(compressed_str)
4. Write a python program that prompts the user to take input strings str, m, n from the console and check if
a given input str starts with string m and ends with string n. If both the conditions are passed, then print
True otherwise print False.
Sample Input:
str= ‘object oriented programming’
m= ‘object’
n= ‘programming’
Sample Output:
True
Solution:
input_str = input("Enter the main string: ")
m = input("Enter the starting substring: ")
n = input("Enter the ending substring: ")
if input_str.startswith(m) and input_str.endswith(n):
print(True)
else:
print(False)
5. Solution: Convert numeric words to numbers:
Given a string S, containing numeric words, the task is to convert the given string to
the actual number.
Sample Input: S = “zero four zero one”
Sample Output: 0401
Solution:
input_str = input("Enter the numeric words separated by spaces: ")
words = input_str.split()
number = ""
for word in words:
if word == "zero":
number += "0"
elif word == "one":
number += "1"
elif word == "two":
number += "2"
elif word == "three":
number += "3"
elif word == "four":
number += "4"
elif word == "five":
number += "5"
elif word == "six":
number += "6"
elif word == "seven":
number += "7"
elif word == "eight":
number += "8"
elif word == "nine":
number += "9"
print(number)