Department of Computer Science and Engineering
PES University, Bangalore, India
Lecture Notes
Python for Computational Problem Solving
UE23CS151A
Lecture #43
Problem Solving using Strings
By,
Prof. Sindhu R Pai,
Anchor, PCPS - 2023
Assistant Professor
Dept. of CSE, PESU
Verified by,
PCPS Team - 2023
Many Thanks to
Dr. Shylaja S S (Director, CCBD and CDSAML Research Centers, Former
Chairperson, CSE, PES University)
Prof. Chitra G M (Asst. Prof, Dept. of CSE, PCPS Anchor – 2022)
Aug – Dec, 2023
Problems on Strings:
Solutions are available in this link: https://drive.google.com/file/d/1-
Ys3gGVtIgUSFjmxBjzVwfKhlVy3bz-U/view?usp=drive_link
1. Program to input a string and print the squares of all the numbers in a String. Take
care of special cases.
Input: wha4ts12ap1p
Output: 16 1 4 144 1
2. Program to take two strings and find the Cartesian product of those two.
Input: str1: abc
str2: ab
Output: a,a a,b b,a b,b c,a c,b
3. Program to find the sum of all substrings of a string representing a number.
Input : num = "1234"
Output : 1670
Sum = 1 + 2 + 3 + 4 + 12 + 23 + 34 + 123 + 234 + 1234 = 1670
4. Write a function to accept a string & swap the adjacent characters and make a new string.
5. Program to replace all occurrences of string YOU with I without using extra space.
Input: YOU AND PES
Output: I AND PES
6. Program to find the addition of pair of elements from the given 2 strings entered by the
user.
Input: sindhu
pai
Output: spiani
7. Program to check whether the specified character is available in a string or not without using
builtin functions.
8. Program to reverse a string entered by the user.
9. Program to delete the characters from string1 which are present in string2 and print the new
string.
input: string1: whatsapp
string2: wat
output: hspp
10. Program to find if two strings are anagrams.
Anagrams: Same set of letters with different arrangements. Number of times a letter
present in one string must be equal to the number of times the letter is present on another
string. Examples: elegant man , a gentleman
-END-
Department of CSE, PESU 2