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

python program with string

The document contains several Python programs that perform string manipulations. These include counting vowels, reversing a string, checking for palindromes, converting to title case, replacing words, and finding character frequency. Each program is illustrated with examples and clear function definitions.

Uploaded by

roshnisanthosh.m
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

python program with string

The document contains several Python programs that perform string manipulations. These include counting vowels, reversing a string, checking for palindromes, converting to title case, replacing words, and finding character frequency. Each program is illustrated with examples and clear function definitions.

Uploaded by

roshnisanthosh.m
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Count Vowels in a String

string = input("Enter a string: ")

# Initialize count to 0
vowel_count = 0
vowels = "aeiouAEIOU"

# Count vowels
for char in string:
if char in vowels:
vowel_count += 1

print("Number of vowels in the string:", vowel_count)

Program to Reverse a String

def reverse_string(text):
return text[::-1]

user_input = input("Enter a string: ")

reversed_text = reverse_string(user_input)

print("The reverse of the string", user_input, "is", reversed_text)

Check if a String is a Palindrome

def is_palindrome(text):
return text == text[::-1]

user_input = input("Enter a string: ")

if is_palindrome(user_input):
print("The string", user_input, "is a palindrome.")
else:
print("The string", user_input, "is not a palindrome.")

Convert the given string into title case

def to_title_case(s):

return s.title()

# Example

text = "hello world, welcome to python programming."

result = to_title_case(text)

print("Original text:", text)

print("Title case:", result)

Replace All Occurrences of a Word in a String

def replace_word(s, old, new):

return s.replace(old, new)

# Example

text = "I love Python. Python is fun!"

new_text = replace_word(text, "Python", "programming")

print("Original text:", text)

print("Updated text:", new_text)

Find the Frequency of a Character in a String

def char_frequency(s, char):

return s.count(char)

# Example
text = "banana"

character = "a"

result = char_frequency(text, character)

print("The character '" + character + "' appears", result, "times in '" + text + "'.")

You might also like