0% found this document useful (0 votes)
7 views11 pages

Chapter 9 Advanced Python Programs

The document contains multiple Python programs that demonstrate various string manipulations, including calculating string length, removing characters at odd indices, counting words and characters, checking for palindromes, and identifying pangrams. Each program includes a problem description, solution steps, source code, and runtime test cases. The examples illustrate how to implement these functionalities without using built-in functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views11 pages

Chapter 9 Advanced Python Programs

The document contains multiple Python programs that demonstrate various string manipulations, including calculating string length, removing characters at odd indices, counting words and characters, checking for palindromes, and identifying pangrams. Each program includes a problem description, solution steps, source code, and runtime test cases. The examples illustrate how to implement these functionalities without using built-in functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Leela Soft 1000 MCQ Programs Madhu

Case 2:
Enter string:apple orange banana
Modified string:
apple-orange-banana

Python Program to Calculate the Length of a String Without Using a Library Function
Problem Description
The program takes a string and calculates the length of the string without using library functions.

Problem Solution
1. Take a string from the user and store it in a variable.
2. Initialize a count variable to 0.
3. Use a for loop to traverse through the characters in the string and increment the count
variable each time.
4. Print the total count of the variable.
5. Exit.

string=input("Enter string:")
count=0
for i in string:
count=count+1
print("Length of the string is:")
print(count)

Program Explanation
1. User must enter a string and store it in a variable.
2. The count variable is initialized to zero.
3. The for loop is used to traverse through the characters in the string.
4. The count is incremented each time a character is encountered.
5. The total count of characters in the string which is the length of the string is printed.

Runtime Test Cases

Case 1:
Enter string:Hello
Length of the string is:
5

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Case 2:
Enter string:Bangalore
Length of the string is:
9

Python Program to Remove the Characters of Odd Index Values in a String


Problem Description
The program takes a string and removes the characters of odd index values in the string.

Problem Solution
1. Take a string from the user and store it in a variable.
2. Pass the string as an argument to a function.
3. In the function, initialize a variable to an empty character.
4. Use a for loop to traverse through the string.
5. Use an if statement to check if the index of the string is odd or even.
6. If the index is odd, append the no character to the string.
7. Then print the modified string.
8. Exit.

Program/Source Code
def modify(string):
final = ""
for i in range(len(string)):
if i % 2 == 0:
final = final + string[i]
return final
string=input("Enter string:")
print("Modified string is:")
print(modify(string))

Program Explanation
1. User must enter a string and store it in a variable.
2. This string is passed as an argument to a function.
3. In the function, a variable is initialized with an empty character.
4. A for loop is used to traverse through the string.
5. An if statement checks if the index of the string is odd or even.
6. If the index is odd, the empty character variable is appended to the string thus indirectly
removing the character.
7. Finally the modified string is printed.

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Runtime Test Cases

Case 1:
Enter string:hello
Modified string is:
hlo

Case 2:
Enter string:checking
Modified string is:
cekn
Python Program to Calculate the Number of Words and the Number of Characters Present in a
String
Problem Description
The program takes a string and calculates the number of words and characters present in the
string.

Problem Solution
1. Take a string from the user and store it in a variable.
2. Initialize the character count variable to 0 and the word count variable to 1.
3. Use a for loop to traverse through the characters in the string and increment the character
count variable each time.
4. Increment the word count variable only if a space is encountered.
5. Print the total count of the characters and words in the string.
6. Exit.

Program/Source Code

string=input("Enter string:")
char=0
word=1
for i in string:
char=char+1
if(i==' '):
word=word+1
print("Number of words in the string:")
print(word)
print("Number of characters in the string:")
print(char)

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Program Explanation
1. User must enter a string and store it in a variable.
2. The character count variable is initialized to zero and the word count variable is initialized
to 1 (to account for the first word).
3. The for loop is used to traverse through the characters in the string.
4. The character count is incremented each time a character is encountered and the word
count is incremented only when a space is encountered.
5. The total count of characters and the words in the string is printed.

Runtime Test Cases


Case 1:
Enter string:Hello world
Number of words in the string:
2
Number of characters in the string:
11

Case 2:
Enter string:I love python
Number of words in the string:
3
Number of characters in the string:
13
Python Program to Take in Two Strings and Display the Larger String without Using Built-in
Functions
Problem Description
The program takes in two strings and display the larger string without using built-in function.

Problem Solution
1. Take in two strings from the user and store it in separate variables.
2. Initialize the two count variables to zero.
3. Use a for loop to traverse through the characters in the string and increment the count
variables each time a character is encountered.
4. Compare the count variables of both the strings.
5. Print the larger string.
6. Exit.

Program/Source Code
Here is source code of the Python Program to take in two strings and display the larger string
without using built-in functions. The program output is also shown below.

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

string1=input("Enter first string:")


string2=input("Enter second string:")
count1=0
count2=0
for i in string1:
count1=count1+1
for j in string2:
count2=count2+1
if(count1<count2):
print("Larger string is:")
print(string2)
elif(count1==count2):
print("Both strings are equal.")
else:
print("Larger string is:")
print(string1)

Program Explanation
1. User must enter two strings and store it in separate variables.
2. The count variables are initialized to zero.
3. The for loop is used to traverse through the characters in the strings.
4. The count variables are incremented each time a character is encountered.
5. The count variables are then compared and the larger string is printed.

Runtime Test Cases

Case 1:
Enter first string:Bangalore
Enter second string:Delhi
Larger string is:
Bangalore

Case 2:
Enter first string:Ball
Enter second string:Apple
Larger string is:
Apple
Python Program to Count Number of Lowercase Characters in a String
Problem Description
The program takes a string and counts number of lowercase characters in a string.

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Problem Solution
1. Take a string from the user and store it in a variable.
2. Initialize a count variable to 0.
3. Use a for loop to traverse through the characters in the string and increment the count
variable each time a lowercase character is encountered.
4. Print the total count of the variable.
5. Exit.

Program/Source Code
Here is source code of the Python Program to count number of lowercase characters in a string.
The program output is also shown below.

string=input("Enter string:")
count=0
for i in string:
if([Link]()):
count=count+1
print("The number of lowercase characters is:")
print(count)

Program Explanation
1. User must enter a string and store it in a variable.
2. The count variable is initialized to zero.
3. The for loop is used to traverse through the characters in the string.
4. The count is incremented each time a lowercase character is encountered.
5. The total count of lowercase characters in the string is printed.

Runtime Test Cases

Case 1:
Enter string:Hello
The number of lowercase characters is:
4

Case 2:
Enter string:AbCd
The number of lowercase characters is:
2
Python Program to Check if a String is a Palindrome or Not
Problem Description
The program takes a string and checks if a string is a palindrome or not.

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Problem Solution
1. Take a string from the user and store it in a variable.
2. Reverse the string using string slicing and compare it back to the original string.
3. Print the final result
4. Exit.

Program/Source Code
Here is source code of the Python Program to check if a string is a palindrome or not. The program
output is also shown below.

string=input("Enter string:")
if(string==string[::-1]):
print("The string is a palindrome")
else:
print("The string isn't a palindrome")

Program Explanation
1. User must enter a string and store it in a variable.
2. The string is reversed using string slicing and it is compared back to the non-reversed
string.
3. If they both are equal, the strings are palindromes.
4. If they aren’t equal, the strings aren’t palindromes.
5. The final result is printed.

Runtime Test Cases

Case 1:
Enter string:malayalam
The string is a palindrome

Case 2:
Enter string:hello
The string isn't a palindrome
Python Program to Calculate the Number of Upper Case Letters and Lower Case Letters in a String
Problem Description
The program takes a string and counts the number of lowercase letters and uppercase letters in
the string.

Problem Solution
1. Take a string from the user and store it in a variable.

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

2. Initialize the two count variables to 0.


3. Use a for loop to traverse through the characters in the string and increment the first
count variable each time a lowercase character is encountered and increment the second count
variable each time a uppercase character is encountered.
4. Print the total count of both the variables.
5. Exit.

Program/Source Code
Here is source code of the Python Program to count the number of lowercase characters and
uppercase characters in a string. The program output is also shown below.

string=input("Enter string:")
count1=0
count2=0
for i in string:
if([Link]()):
count1=count1+1
elif([Link]()):
count2=count2+1
print("The number of lowercase characters is:")
print(count1)
print("The number of uppercase characters is:")
print(count2)

Program Explanation
1. User must enter a string and store it in a variable.
2. Both the count variables are initialized to zero.
3. The for loop is used to traverse through the characters in the string.
4. The first count variable is incremented each time a lowercase character is encountered
and the second count variable is incremented each time a uppercase character is encountered.
5. The total count of lowercase characters and uppercase characters in the string are
printed.

Runtime Test Cases

Case 1:
Enter string:HeLLo
The number of lowercase characters is:
2
The number of uppercase characters is:
3

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Case 2:
Enter string:Leelasoft
The number of lowercase characters is:
7
The number of uppercase characters is:
3
Python Program to Check if a String is a Pangram or Not
Problem Description
The program takes a string and checks if it is a pangram or not.

Problem Solution
1. Take a string from the user and store it in a variable.
2. Pass the string as an argument to a function.
3. In the function, form two sets- one of all lower case letters and one of the letters in the
string.
4. Subtract these both sets and check if it is equal to an empty set.
5. Print the final result.
6. Exit.

Program/Source Code
Here is source code of the Python Program to check if a string is a pangram or not. The program
output is also shown below.

from string import ascii_lowercase as asc_lower


def check(s):
return set(asc_lower) - set([Link]()) == set([])
strng=input("Enter string:")
if(check(strng)==True):
print("The string is a pangram")
else:
print("The string isn't a pangram")
Program Explanation
1. User must enter a string and store it in a variable.
2. The string is passed as an argument to a function.
3. In the function, two sets are formed- one for all lower case letters and one for the letters
in the string.
4. The two sets are subtracted and if it is an empty set, the string is a pangram.
5. The final result is printed.

Runtime Test Cases

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Case 1:
Enter string:The quick brown fox jumps over the lazy dog
The string is a pangram

Case 2:
Enter string:Hello world
The string isn't a pangram

Python Program to Accept a Hyphen Separated Sequence of Words as Input and Print the Words
in a Hyphen-Separated Sequence after Sorting them Alphabetically
Problem Description
The program accepts a hyphen separated sequence of words as input and print the words in a
hyphen-separated sequence after sorting them alphabetically.

Problem Solution
1. Take a hyphen separated sequence of words from the user.
2. Split the words in the input with hyphen as reference and store the words in a list.
3. Sort the words in the list.
4. Join the words in the list with hyphen between them and print the sorted sequence.
5. Exit.

Program/Source Code
Here is source code of the Python Program to accept a hyphen separated sequence of words as
input and print the words in a hyphen-separated sequence after sorting them alphabetically. The
program output is also shown below.

print("Enter a hyphen separated sequence of words:")


lst=[n for n in input().split('-')]
[Link]()
print("Sorted:")
print('-'.join(lst))

Program Explanation
1. User must enter a hyphen separated sequence of words as the input.
2. The sequence is split with the hyphen as the key and the words are stored in a list.
3. The words in the list are sorted alphabetically using the sort() function.
4. The words in the list are then joined using hyphen as the reference.
5. The sorted sequence of words is then printed.

Runtime Test Cases

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Case 1:
red-green-blue-yellow
Sorted:
blue-green-red-yellow

Case 2:
Enter a hyphen separated sequence of words:
Bangalore-hyderabad-delhi
Sorted:
Bangalore-delhi-hyderabad

Python Program to Calculate the Number of Digits and Letters in a String


Problem Description
The program takes a string and calculates the number of digits and letters in a string.

Problem Solution
1. Take a string from the user and store it in a variable.
2. Initialize the two count variables to 0.
3. Use a for loop to traverse through the characters in the string and increment the first
count variable each time a digit is encountered and increment the second count variable each
time a character is encountered.
4. Print the total count of both the variables.
5. Exit.

Program/Source Code
Here is source code of the Python Program to calculate the number of digits and letters in a string.
The program output is also shown below.

string=input("Enter string:")
count1=0
count2=0
for i in string:
if([Link]()):
count1=count1+1
count2=count2+1
print("The number of digits is:")
print(count1)
print("The number of characters is:")
print(count2)

[Link] Cell: 78 42 66 47 66

You might also like