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

String Manipulation and Counting Tasks

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)
9 views2 pages

String Manipulation and Counting Tasks

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

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

txt='DIvIsiBiLItY'
count=0
for i in txt:
if [Link]()=='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 [Link]()=='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=[Link]()
txt_list=[Link](" ")
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=[Link]()
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=[Link]()
txt_list=[Link](" ")
print(len(txt_list))

Common questions

Powered by AI

The result of this transformation will be 'diVisibIlity'. Each character is checked if it is between lowercase 'a' to 'z' or not, adjusting its ASCII value accordingly to switch between lowercase and uppercase.

By iterating through each character in 'DIvIsiBiLItY' and printing each with a '$' following it, the result would be 'D$I$v$I$s$i$B$I$L$I$t$Y$'. This approach uses a loop to concatenate each character with '$' in sequence.

The process involves converting the sentence to lowercase, splitting it into a list of words at spaces, and counting the elements (words) in this list, resulting in a total of 15 words.

The letter 'I' appears 4 times in the string 'DIvIsiBiLItY'. This is determined by iterating over each character, converting it to lowercase, and checking if it matches 'i'.

The output would be 'D v s B L tY'. This is achieved by iterating over the string and appending a space to a new string whenever 'i' is encountered, regardless of its case.

There are 3 articles in the sentence: 'A', 'an', and 'the'. This is determined by checking each word in the sentence against a list of article words.

There are 17 vowels in the sentence. This count is obtained by iterating through the sentence and checking each character against a list of vowels ('a', 'e', 'i', 'o', 'u')

You might also like