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

String Manipulation

The document contains a series of practical programming exercises focused on string manipulation in Python. Each practical includes a specific task, such as counting character occurrences, replacing vowels, reversing strings, validating phone numbers, estimating word counts, counting vowels, finding the longest word, and reversing words in a sentence. Sample user inputs and outputs are provided for each practical to illustrate the expected results.
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)
8 views

String Manipulation

The document contains a series of practical programming exercises focused on string manipulation in Python. Each practical includes a specific task, such as counting character occurrences, replacing vowels, reversing strings, validating phone numbers, estimating word counts, counting vowels, finding the longest word, and reversing words in a sentence. Sample user inputs and outputs are provided for each practical to illustrate the expected results.
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/ 21

String

Manipulation
Practical 1
Q) Write a program to count the
number of times a character
occurs in the given string.
# Get user input
user_string = input("Enter a
string: ")
user_character = input("Enter the
character to count: ")

# Ensure the user inputs a single


character
if len(user_character) != 1:
print("Please enter a single
character.")
else:
# Initialize a counter
count = 0

# Loop through each character


in the string
for char in user_string:
if char == user_character:
count += 1

# Print the result


print(f"The character
'{user_character}' occurs {count}
times in the string.")
Result
Enter a string: Divisibility
Enter the character to count: i
The character 'i' occurs 5 times in
the string.

Practical 2
Q) Write a program which
replaces all vowels in the string
with '*'.
# Get user input
user_string = input("Enter a
string: ")

# Define the vowels


vowels = "aeiouAEIOU"

# Replace vowels with '*'


modified_string = ""
for char in user_string:
if char in vowels:
modified_string += '*'
else:
modified_string += char

# Print the modified string


print("Modified string:",
modified_string)

Result
Enter a string: thermodynamics
Modified string: th*rm*dyn*m*cs

Practical 3
Q) Write a program which reverses
a string and stores the reversed
string in a new string.
# Get user input
user_string = input("Enter a
string: ")

# Initialize an empty string to


store the reversed string
reversed_string = ""

# Loop through the original string


in reverse order
for char in user_string:
reversed_string = char +
reversed_string # Prepend the
character to the reversed string
# Print the reversed string
print("Reversed string:",
reversed_string)

Result
Enter a string: Knowledge
Reversed string: egdelwonK

Practical 4
Q) Write a program that prompts
for a phone number of 10 digits
and two dashes, with dashes after
the area code and the next three
numbers.

# Prompt for phone number


phone_number = input("Enter a
phone number (format: XXX-XXX-
XXXX): ")

# Check if the phone number is in


the correct format
if (len(phone_number) == 12 and
phone_number[3] == '-' and
phone_number[7] == '-' and
phone_number[:3].isdigit() and
phone_number[4:7].isdigit()
and
phone_number[8:].isdigit()):

print("Valid phone number


entered:", phone_number)
else:
print("Invalid phone number
format. Please use the format
XXX-XXX-XXXX.")
Result
Enter a phone number (format:
XXX-XXX-XXXX): 123-456-7890
Valid phone number entered: 123-
456-7890
Practical 5
Q) Write a program that asks the
user the user for a string (only
single space between words) and
returns an estimate of how many
words are in the string.
# Ask the user for input
user_input = input("Please enter a
string (only single spaces
between words): ")

# Strip leading and trailing spaces


stripped_string =
user_input.strip()

# Check if the string is empty


if not stripped_string:
word_count = 0 # No words if
the string is empty after stripping
else:
# Count the number of spaces
and add one to estimate the
number of words
word_count =
stripped_string.count(' ') + 1

# Print the estimated number of


words
print(f"Estimated number of
words: {word_count}")
Result
Please enter a string (only single
spaces between words): python
tuples are immutable
Estimated number of words: 4
Practical 6
Q) Write a program that inputs a
line of text and print out the count
of vowels in it.
# Ask the user for input
user_input = input("Please enter a
line of text: ")

# Define the vowels


vowels = "aeiouAEIOU"

# Initialize a counter for vowels


vowel_count = 0
# Iterate through each character
in the input string
for char in user_input:
if char in vowels:
vowel_count += 1

# Print the count of vowels


print(f"Number of vowels in the
input text: {vowel_count}")
Result
Please enter a line of text: the if
statement is a conditional
statement
Number of vowels in the input
text: 15
Practical 7
Q) Write a program to input a line
of text and print the biggest word
(length wise) from it.
# Ask the user for input
user_input = input("Please enter a
line of text: ")

# Split the input string into words


words = user_input.split()

# Initialize variables to track the


longest word
longest_word = ""
max_length = 0

# Iterate through each word in


the list
for word in words:
# Check if the current word is
longer than the longest found so
far
if len(word) > max_length:
longest_word = word
max_length = len(word)
# Print the longest word
if longest_word:
print(f"The longest word is:
'{longest_word}' with length
{max_length}.")
else:
print("No words found in the
input.")
Result
Please enter a line of text: The
Python lists are containers that
are used to store a list of values
of any type
The longest word is: 'containers'
with length 10.
Practical 8
Q) Write a program to input a line
of text and create a new line of
text where each word of input line
is reversed.
# Ask the user for input
user_input = input("Please enter a
line of text: ")
# Split the input string into words
words = user_input.split()

# Reverse each word and store


them in a new list
reversed_words = [word[::-1] for
word in words]

# Join the reversed words into a


new line of text
reversed_line = '
'.join(reversed_words)

# Print the new line of text


print("Reversed words line:",
reversed_line)
Result
Please enter a line of text:
Primary memory is the internal
volatile memory where data and
instructions are stored during
processing
Reversed words line: yramirP
yromem si eht lanretni elitalov
yromem erehw atad dna
snoitcurtsni era derots gnirud
gnissecorp

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy