The document contains 21 programming problems related to strings in Python. The problems involve tasks like prompting the user for input strings, counting characters, reversing strings, replacing characters, encrypting/decrypting strings, and generating sentences from lists of words. Hints are provided for some problems, like finding the maximum width of each column when printing a table of strings.
1. Techno India University,W.B.
Department of Computer Applications
Subject:-Tools and Techniques of Programming
using Python
ASSIGNMENT-4
Topic: Strings in Python
December 19, 2021
Develop Python scripts to solve following problems:
1. A program is required to be written that prompts the user to enter
a string. The program should then print the following:
(a) The total number of characters in the string
(b) The string repeated 5 times
(c) The first character of the string
(d) The first four characters of the string
(e) The last two characters of the string
(f) The string in the reversed form
(g) The ninth character of the string if the string is long enough
and a message otherwise
(h) The string with its first and last characters removed
(i) The string in all the letters in capital form
(j) The string with every ‘i’ replaced with an ‘I’
(k) The string with every letter replaced by a space
2. A program is required to be written that prompts the user to enter
a string and returns the number of words in the string.
1
2. 3. It has been observed that programmers often forget to type the
closing parentheses when formulas are entered. Develop a program
that asks the user to enter a formula and prints out whether the
entered formula contains the same number of opening and closing
parentheses.
4. Develop a program that asks the user to enter a string and prints
out whether the string contains any vowel or not.
5. Develop a program that asks the user to enter a string to create a
new string called new_str from the given string such that the third
character is changed to an asterisk and two signs of exclamation are
attached to the end of the string. Finally, the program prints out
the new_str.
6. Develop a program that prompts the user to enter a string str for
its conversion to lowercase by removing all the periods and commas
from it, and shows the resulting string.
7. A program is required to be developed for the following outputs out
of a given string: for counting the number of sentences in the given
string and for counting the number of lines in the given string.
8. In a certain university named TIU, the email addresses of the stu-
dents end with @student.tiu.edu, while the email addresses of the
professors end with @prof.tiu.edu. It is required to develop a pro-
gram that first asks the user the number of email addresses he/she
will enter, and then it will allow the user enter those addresses. Af-
ter completion of the entry, the program is required to print out a
message showing the number of entered email address belonging to
each category.
9. Develop a program that prompts the user to input a string, then
prints out each letter of the string tripled in a separate line. For
instance, if the input is DAD , the output would be DDD AAA
DDD
10. A program is required to be developed for deciphering a given string
of code numbers into its equivalent alphabetic letters by using the
following rules:
2
3. (a) A number is to be treated as an ASCII decimal value of a char-
acter
(b) 5 is to be added to the value before converting it into a letter
(c) If addition of 5 takes the new value to some value that exceeds
the range of letters then 10 is to be subtracted from the sum for
translating it into a letter.
(d) Spaces in it will remain spaces.
(e) Three digits are to be taken at a time for the translation.
(f) Finally , the translated code is to be shown.
11. Develop a program that prompts the user to enter a word that
contains a chosen letter given as input. If the letter is not contained
in the word, then the program is required to generate a message
regarding that; otherwise, it will print the part of the word up to
that given letter in a separate line and the rest in the next line. For
example, if the input word is ‘Coward’ and the given letter is ‘w’,
then the output would look like the following one: Coward
12. Develop a program that prompts the user to enter a word; it then
capitalizes every alternate letter of that word and shows the result-
ing word. So if the input word is ‘elephant’, then the out will be:
eLePhAnT.
13. It is required to develop a program that prompts the user to enter
two strings of equal length. The program then checks to see if the
strings are really of the same length. If they are not, the program
should then print an appropriate message and exit. If they are of
the same length, the program should then concatenate the strings
by taking alternate characters from the two strings and prints the
resulting string. For instance, if the user enters ‘Book’ and ‘Star’
the output should be BSotoakr.
14. Let us have a long string of a few sentences as input in a program
with some missing words represented with hyphens. The program
will then asks for a few types of words for insertion in places of the
hyphens. The program then replaces the hyphens with the given
words and prints the reconstructed text. The resulting string may
3
4. be a funny one. Nevertheless, we need to develop the code. For
instance, Let the input text be: Tiger is a – animal. It likes to eat
-. It lives in -.
Now, the questions may be like the following ones:
Enter an adjective: happy
Enter the name of a food item: noodles
Enter the name of a place: city
Now, we have the following reconstructed string: Tiger is a happy
animal. It likes to eat noodles. It lives in city.
15. Develop a program for the following activities:
(a) Without using the in operator, the program asks the user for
a string and a letter and prints out whether or not the letter
appears in the string.
(b) Without using the count method, the program asks the user
for a string and a letter and counts how many times the letter
occurs in the string.
(c) Without using the index method, the program that asks the
user for a string and a letter and prints out the index of the
first occurrence of the letter in the string. If the letter is not in
the string, the program should say so.
16. Write a program that asks the user for a large integer and inserts
commas into it according to the standard convention for commas in
large numbers. For instance, if the user enters 12345678, the output
should be 12,345,678.
17. A program is required to be developed for generating all possible
permutations of words with all the letters in the word. For example,
if the given word is ‘bit’, the different possible words will be: bit,
tib, tbi, bti, tib, ibt.
18. One simple way of encrypting a message is by rearranging its char-
acters. One way to rearrange the characters is to pick out the char-
acters at odd indices, put them first in the encrypted string, and
follow them by the characters at the even positions. For instance,
the message ‘Save our Soul’ would be encrypted as: Sv u oliaeorSu
4
5. (a) Develop a program that prompts the user for a string and uses
this method to encrypt the string to show the encrypted message.
(b) Develop a program to decrypt a string that was encrypted with
this method. 19. A more general version of the above technique
is the rail fence cipher, where instead of breaking things into evens
and odds, they are broken up by threes, fours or something larger.
(a) Develop a program the prompts the user for a string and uses
the rail fence cipher in the threes case to encrypt the string.
(b) Write a decryption program for the threes case.
(c) Write a program that asks the user for a string, and an integer
determining whether to break things up by threes, fours, or
whatever. Encrypt the string using the rail-fence cipher.
(d) Write a decryption program for the general case.
19. In calculus, the derivative of x5 is 5x4. The derivative of x3 is 3x2.
This pattern continues. Develop a program that prompts the user
for input like x3
or x2
5 and shows the derivative.
20. In algebraic expressions, the symbol for multiplication is often omit-
ted, as in 5x+7y or5(x+3). Computers cannot operate on those
expressions. It is required to develop a program that prompts the
user for an algebraic expression and then inserts the multiplication
symbols where appropriate.
21. Develop a Python script named showTable that takes a list of lists
of strings for
(a) Printing it in a well-organized table with each column right-
justified. Assume that all the inner lists will contain the same
number of strings. For example, the value could look like this:
tableData = [[’Jasmine’, ’Rose, ’Tulip’, ’Chrysanthe-
mum’], [’Julia’, ’Jahanara’, ’Manasi’, ’Tapasi’],
[’is akin to’, ’resembles’, ’reminds me the name of’, ’is
another name for’]]
(b) Make sentences by combining each word from the second list
with the corresponding phrase in the second list followed by
the corresponding word from the first list.For example, the first
sentence would be: Julia is akin to Jasmin.
5
6. Hint for part 1: The code may first find out the longest string in each of the inner lists so that the whole column can be made
wide enough to fit all the strings. The maximum width of each column can be stored as a list of integers. The printTable()
function can begin with colWidths = [0] * len(tableData), which will create a list containing the same number of 0 values
as the number of inner lists in tableData. That way, colWidths[0] can store the width of the longest string in tableData[0],
colWidths[1] can store the width of the longest string in tableData[1], and so on. Then , the largest value in the colWidths
list can be determined to find out what integer width to pass to the rjust() string method.
6