Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (1 vote)
155 views

Python String Program

The document contains Python programs to perform various string operations: replacing characters in a string, counting vowels, replacing spaces with hyphens, counting words and characters, checking for palindromes, counting uppercase and lowercase letters, counting digits and letters, counting word occurrences, and checking for substrings.

Uploaded by

subratdash90
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
155 views

Python String Program

The document contains Python programs to perform various string operations: replacing characters in a string, counting vowels, replacing spaces with hyphens, counting words and characters, checking for palindromes, counting uppercase and lowercase letters, counting digits and letters, counting word occurrences, and checking for substrings.

Uploaded by

subratdash90
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Replace all Occurrences of ‘a’ with $ in a String

string=input("Enter string:")
string=string.replace('a','$')
string=string.replace('A','$')
print("Modified string:")
print(string)

Count the Number of Vowels in a String


string=input("Enter string:")
vowels=0
for i in string:
if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I' or i=='O' or i=='U'):
vowels=vowels+1
print("Number of vowels are:")
print(vowels)

Python Program to Take in a String and Replace Every Blank Space with Hyphen
string=input("Enter string:")
string=string.replace(' ','-')
print("Modified string:")
print(string)

Python Program to Calculate the Number of Words and the Number of Characters Present in a 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)

Python Program to Check if a String is a Palindrome or Not


string=input("Enter string:")
if(string==string[::-1]):
print("The string is a palindrome")
else:
print("The string isn't a palindrome")
Python Program to Calculate the Number of Upper Case Letters and Lower Case Letters in a 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)

Python Program to Calculate the Number of Digits and Letters in a String


string=input("Enter string:")
count1=0
count2=0
for i in string:
if(i.isdigit()):
count1=count1+1
count2=count2+1
print("The number of digits is:")
print(count1)
print("The number of characters is:")
print(count2)

Python Program to Count the Occurrences of Each Word in a Given String Sentence
string=input("Enter string:")
word=input("Enter word:")
a=[]
count=0
a=string.split(" ")
for i in range(0,len(a)):
if(word==a[i]):
count=count+1
print("Count of the word is:")
print(count)

Python Program to Check if a Substring is Present in a Given String

string=input("Enter string:")
sub_str=input("Enter word:")
if(string.find(sub_str)==-1):
print("Substring not found in string!")
else:
print("Substring in string!")

You might also like