0% found this document useful (0 votes)
3 views9 pages

Practical File

The document provides a series of Python programs focused on string manipulation tasks, including finding character locations, doubling characters, counting occurrences, replacing vowels, reversing strings, validating phone numbers, extracting digits, and analyzing sentences. Each program includes a description, code snippet, and example output. The tasks are designed to enhance understanding of string operations in Python.
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)
3 views9 pages

Practical File

The document provides a series of Python programs focused on string manipulation tasks, including finding character locations, doubling characters, counting occurrences, replacing vowels, reversing strings, validating phone numbers, extracting digits, and analyzing sentences. Each program includes a description, code snippet, and example output. The tasks are designed to enhance understanding of string operations in Python.
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

STRING MANIPULATION

[Link]. Programs

1. Write a program that asks the user for a string a and a character c ; and then it prints out the
location of each character c in the string s.

Program
s=input("Enter a string : ")
c=input(" enter the character whose location you want to see :")
for i in range(len(s)):
if s[i]==c:
print(i)
Output
Enter a string : Hola amigo
enter the character whose location you want to see :a
3
5

2. Write a program that asks the user for a string and creates a new string that doubles each
character of the original string.

Program
a=input("Enter a string : ")
b=""
for x in a:
b=b+x*2
print("The new string is : ",b)
Output
Enter a string : Pizza
The new string is : PPiizzzzaa

3. Write a program to count the number of times a character occurs in the given string.

Program
str = input("Enter the string: ")
ch = input("Enter the character to count: ");
c = [Link](ch)
print(ch, "occurs", c, "times")
Output
Enter the string: Computer Science
Enter the character to count: e
e occurs 3 times

4. Write a program which replaces all vowels in the string with “*”.
Program
s = input("Enter the string: ")
n = ""
for i in s :
l = [Link]()
if l == 'a' \
or l == 'e' \
or l == 'i' \
or l == 'o' \
or l == 'u' :
n += '*'
else :
n += i
print(n)

Output
Enter the string: Computer Science
C*mp*t*r Sc**nc*

5. Write a program which reverses a string and stores the reversed string in a new string.

Program
a = input("Enter the string: ")
b = ""
for i in a :
b=i+b
print(b)

Output
Enter the string: "Hello World"
"dlroW olleH"

6. Write a program that prompts for a phone number of 10 digits and two dashes , with dashes
after the area code and the next three numbers.
Program
phNo = input("Enter the phone number: ")
length = len(phNo)
if length == 12 \
and phNo[3] == "-" \
and phNo[7] == "-" \
and phNo[:3].isdigit() \
and phNo[4:7].isdigit() \
and phNo[8:].isdigit() :
print("Valid Phone Number")
else :
print("Invalid Phone Number")

Output
Enter the phone number: 070-9161-9432
Invalid Phone Number
Enter the phone number: 070-898-9132
Valid Phone Number

7. Write a program that should do the following:


Prompt the user for a string

extract all the digits from the string

If there are digits:

Sum the collected digits together

Print out:
Original string the digits the sum of the digits
If there are no digits

Print the original string and a message “has no digits”

Program
str = input("Enter the string: ")
sum = 0
digitStr = ''
for ch in str :
if [Link]() :
digitStr += ch
sum += int(ch)
if not digitStr :
print(str, "has no digits")
else :
print(str, "has the digits", digitStr, "which sum to", sum)
Output

Enter the string: Computer6532

Computer6532 has the digits 6532 which sum to 16

8. Write a program that should prompt the user to type some sentences followed by “enter”
. It should then print the original sentence and the following statistics relating to the
sentences:
Number of words

Number of characters

Percentage of characters that are alphanumeric

Program
str = input("Enter a few sentences: ")
length = len(str)
spaceCount = 0
alnumCount = 0

for ch in str :
if [Link]() :
spaceCount += 1
elif [Link]() :
alnumCount += 1

alnumPercent = alnumCount / length * 100

print("Original Sentences:")
print(str)

print("Number of words =", (spaceCount + 1))


print("Number of characters =", (length + 1))
print("Alphanumeric Percentage =", alnumPercent)
Output

Enter a few sentences: "Hello my name is Hritwik I am 16 years old"

Original Sentences:

"Hello my name is Hritwik I am 16 years old"

Number of words = 10

Number of characters = 45
Alphanumeric Percentage = 75.0

9. Write a Python program as per specifications given below:


Repeatedly prompt for a sentence(string) or for “q” to quit.

Upon input of a sentence s , print the string produced from s by converting each lower
case letter to upper case and each upper case letter to lower case.
All other characters are left unchanged.

Program
while True :
str = input("Please enter a sentence, or 'q' to quit : ")
newStr = ""
if [Link]() == "q" :
break
for ch in str :
if [Link]() :
newStr += [Link]()
elif [Link]() :
newStr += [Link]()
else :
newStr += ch
print(newStr)

Output

Please enter a sentence, or 'q' to quit : Computer Science

cOMPUTER sCIENCE

Please enter a sentence, or 'q' to quit : HeLlO

hElLo

Please enter a sentence, or 'q' to quit : q


10. Write a program that does the following:
Takes two inputs : the first , an integer and the second, a string

From the input string extract all the digits, in the order they occurred, from the string.

If no digits occur, set the extracted digits to 0

Add the integer input and the digits extracted from the string together as integers

Print a string of the form :


“integer_input + string_digits=sum’

Program

num = int(input("Enter an integer: "))

str = input("Enter the string: ")

digitsStr = ''

digitsNum = 0;

for ch in str :

if [Link]() :

digitsStr += ch

if digitsStr :

digitsNum = int(digitsStr)

print(num, "+", digitsNum, "=", (num + digitsNum))

Output

Enter an integer: 9

Enter the string: qlpu563

9 + 563 = 572

11. Write a program to convert a given number into equivalent Roman number (store its value
as a string).
Program
n = int(input("Enter the number: "))
num = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)
rom = ('M', 'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I')

result = ''

for i in range(len(num)) :
count = int(n / num[i])
result += str(rom[i] * count)
n -= num[i] * count

print(result)

Output

Enter the number: 2015

MMXV

12. Write a program that asks the user for a string (only single space between words) and returns
an estimated of how many words are in the string.

Program
str = input("Enter a string: ")
count = 0
for ch in str :
if [Link]() :
count += 1
print("No of words =", (count + 1))

Output
Enter a string: Hello World
No of words = 2

13. Write a program that inputs a line of text and prints out the count of vowels in it.
Program
str = input("Enter a string: ")
count = 0

for ch in str :
lch = [Link]()
if lch == 'a' \
or lch == 'e' \
or lch == 'i' \
or lch == 'o' \
or lch == 'u' :
count += 1

print("Vowel Count =", count)

Output

Enter a string: computer science

Vowel Count = 6

14. Write a program to input a line of text and print the biggest word from it.

Program
str = input("Enter a string: ")
words = [Link]()
longWord = ''

for w in words :
if len(w) > len(longWord) :
longWord = w

print("Longest Word =", longWord)

Output

Enter a string: "Computer science with Python"

Longest Word = "Computer

15. Write a program to input a line of text and create a new line of text where each word of the
input line is reversed.

Program
a=input("Enter a string : ")
b=[Link]()
c=""
for i in b :
d = ""
for x in i :
d=x+d
c += d + " "

print(c)
Output
Enter a string : Hello World
olleH dlroW

You might also like