0% found this document useful (0 votes)
2 views

String Manipulation Programs (1)

The document contains four Python code snippets for string manipulation tasks. The first snippet counts uppercase and lowercase letters, the second counts words and characters, the third searches for a substring and returns its index, and the fourth counts the number of words in a string. Each snippet includes user input and prints the results accordingly.

Uploaded by

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

String Manipulation Programs (1)

The document contains four Python code snippets for string manipulation tasks. The first snippet counts uppercase and lowercase letters, the second counts words and characters, the third searches for a substring and returns its index, and the fourth counts the number of words in a string. Each snippet includes user input and prints the results accordingly.

Uploaded by

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

1. To count no.

of uppercase and lowercase letters in the given string

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

2. To count no. of words and characters in the given string

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)

3. To write a program to search a substring in the given string of line and return it’s index

string = input("enter a line of text: ")


substring = input("Enter string to be searched: ")
if substring in string:
position=string.find(substring)
print("The occurence of the substring is: ", position)
print("Substring found!")
else:
print("Substring not found.")
4. To count number of words in a given string
s = input(“Enter String: “)
word_count = len(s.split())
print(word_count)
or
s = input(“Enter String: “)
word_count = 0
for word in s.split():
word_count += 1
print("Word Count:", word_count)

You might also like