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

Python String Manipulation Programs

The document contains various Python programs that demonstrate string manipulation techniques, including counting characters and words, checking for palindromes, and replacing characters. It provides code snippets for tasks such as counting vowels and consonants, changing vowels to asterisks, and performing string operations like splitting and replacing. Each program includes input prompts and expected outputs for clarity.

Uploaded by

SHAJITH STUDIO
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 views6 pages

Python String Manipulation Programs

The document contains various Python programs that demonstrate string manipulation techniques, including counting characters and words, checking for palindromes, and replacing characters. It provides code snippets for tasks such as counting vowels and consonants, changing vowels to asterisks, and performing string operations like splitting and replacing. Each program includes input prompts and expected outputs for clarity.

Uploaded by

SHAJITH STUDIO
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

PYTHON PROGRAMS - STRING

1. PROGRAM TO COUNT NUMBER OF CHARACTERS IN A STRING

a=str(input("Enter the string: "))

count=0

for i in range (0,len(a)):

if (a[i] != " "):

count+=1

print("The number of characters in the string: ",count)

[Link] TO COUNT NUMBER OF WORDS IN A SENTENCE.

a=str(input("Enter the string: "))

b=len([Link]())

print("The number of words in the sentence: ",b)

[Link] a program that takes a sentence as an input parameter


where each word in the sentence is separated by a space. The
function should replace each blank with a hyphen and then return
the modified sentence

a=input("Enter the string: ")

b=[Link](" ", "-")

print(b)

output:

Enter the string: This is my first program

This-is-my-first-program
[Link] TO CHECK WHETHER THE STRING IS PALINDROM OR
NOT

a=str(input("Enter the string: "))


s=a[: :-1]
print(s)
if a==s:
print("The string is a Palindrom")
else:
print("The string is not Palindrom")

[Link] to count no of uppercase, lowercase,digits and other


characters in a given string

a=input("Enter a string: ")


u=l=d=c=0
for i in a:
if [Link]():
u=u+1
elif [Link]():
l=l+1
elif [Link]():
d+=1
else:
c+=1
print("the original string: ",a)
print("No of capital letters: ",u)
print("No of small letters: ",l)
print("No of digits: ",d)
print("No of other characters: ",c)

output:

Enter a string: krm#CoMPutER@1234_454


the original string: krm#CoMPutER@1234_454
No of capital letters: 5
No of small letters: 6
No of digits: 7
No of other characters: 3

[Link] to count number of vowels and consonants of a given


string

a=input("Enter a string: ")


v="aeiouAEIOU"
v=0
c=0
for i in a:
if i in vowles:
v=v+1
else:
c=c+1
print("the original string: ",a)
print("No of vowels: ",v)
print("No of consonants: ",c)

output:
Enter a string: computer science
the original string: computer science
No of vowels: 6
No of consonants: 10
[Link] to change vowels in a string with ‘*’

a=input("Enter a string: ")


s=""

v="aeiouAEIOU"
for i in a:
if i in v:
s=s+"*"
else:
s=s+i
print("the original string: ",a)
print("the changed string: ",s)

output:
Enter a string: krmpublicschool
the original string: krmpublicschool
the changed string: krmp*bl*csch**l

[Link] the following string a:

a = "Computer Science"

What will be the output of the following string operations :


i. print(a[0:len(a)])
ii. print(a[-7:-1])
iii. print(a[::2])
iv. print(a[len(a)-1])
v. print(2*a)
vi. print(a[::-2])
vii. print(a[:3] + a[3:])
viii. print([Link]())
ix. print([Link]('Comp'))
x. print([Link]())
[Link] a program to create a space by replacing vowels

t = 'GFGaBste4oCS'
print("The original string is : " + str(t))

vow="aeiouAEIOU"
for i in t:
if i in vow:
t=[Link](i,"*")
res=[Link]("*")

print("The splitted string : " + str(res))

output
The original string is : GFGaBste4oCS
The splitted string : ['GFG', 'Bst', '4', 'CS']

10.

X = "WZ-1,New Ganga Nagar,New Delhi"


What will be the output of following string operations :
i. print([Link]())
ii. print([Link]())
iii. print([Link]('New'))
iv. print([Link]('New'))
v. print([Link]('New'))
vi. print([Link](','))
vii. print([Link](' '))
viii. print([Link]('New','Old'))
ix. print([Link](','))
x. print([Link]('Agra'))

You might also like