0% found this document useful (0 votes)
4 views2 pages

string prgs

The document contains Python code snippets that perform various string manipulations. It counts occurrences of the letter 'I', replaces 'i' with spaces, separates characters with '$', converts case, counts articles, counts vowels, and counts words in a given string. Each operation is demonstrated with a specific example string.

Uploaded by

zaarakumar01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

string prgs

The document contains Python code snippets that perform various string manipulations. It counts occurrences of the letter 'I', replaces 'i' with spaces, separates characters with '$', converts case, counts articles, counts vowels, and counts words in a given string. Each operation is demonstrated with a specific example string.

Uploaded by

zaarakumar01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#Read a string “DIvIsiBiLItY” and count no. of ‘I’.

txt='DIvIsiBiLItY'
count=0
for i in txt:
if i.lower()=='i':
count+=1
print(count)
#Replace ‘i’ with space. (try with replace function and without replace function)
-----------------------------------------
#replace i with space
txt='DIvIsiBiLItY'
txt1=''
count=0
for i in txt:
if i.lower()=='i':
count+=1
txt1+=' '
else:
txt1+=i
print(txt1)
-------------------------------------------
#Separate each character with ‘$’
txt='DIvIsiBiLItY'
for i in txt:
print(i,end="$")
-----------------------------------
#Convert uppercase case letters to lowercase or vice verse
txt='DIvIsiBiLItY'
txt1=''
for i in txt:
if 'a'<i<'z':
txt1+=chr(ord(i)-32)
else:
txt1+=chr(ord(i)+32)
print(txt1)
--------------------------------------------
#Read a string “A cat sat on the mat near an open window,
#watching a bird on the fence.” Count no. of articles
txt="A cat sat on the mat near an open window,watching a bird on the fence."
txtL=txt.lower()
txt_list=txtL.split(" ")
art=['a','an', 'the']
count=0
for i in txt_list:
if i in art:
count+=1
print(count)
----------------------------------------------
#count no. of vowels
txt="A cat sat on the mat near an open window,watching a bird on the fence."
txtL=txt.lower()
vow="aeiou"
count=0
for i in txtL:
if i in vow:
count+=1
print(count)
--------------------------------------------
#count no, of words
txt="A cat sat on the mat near an open window,watching a bird on the fence."
txtL=txt.lower()
txt_list=txtL.split(" ")
print(len(txt_list))

You might also like