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

10-Program

Uploaded by

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

10-Program

Uploaded by

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

a) Python Program to Remove Punctuations From a String

b) Write a python program to reverse a string using user-defined function.

Python Program to Remove Punctuations From a String

# Function to remove punctuations from a string

def remove_punctuations(input_string):

# List of common punctuations


punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
# Resultant string without punctuations
no_punct = ""
for char in input_string:
# Append only if the character is not a punctuation
if char not in punctuations:
no_punct += char
return no_punct

# Input string from the user


user_input = input("Enter a string: ")

# Call the function and display the result


result = remove_punctuations(user_input)
print("String without punctuations:", result)

Additional Clarity for the written Program, only for understanding, not for writing:

1. Function Definition
def remove_punctuations(input_string):
- The function is defined to take a string input and remove all punctuation marks.
2. Punctuation List
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
- This variable stores common punctuation marks to compare against.
3. Initialize Resultant String
no_punct = ""
- An empty string is initialized to hold the resultant string without punctuations.
4. Iterating Through the Input String
for char in input_string:
- Loops through each character in the input string.
5. Check for Punctuation
if char not in punctuations:
- This condition checks if the character is not in the list of punctuations.
6. Append Non-Punctuation Characters
no_punct += char
- Adds the non-punctuation character to the result string.
7. Return Resultant String
return no_punct
- Returns the string after all punctuations are removed.
8. Take User Input
user_input = input("Enter a string: ")
- Takes a string input from the user.
9. Call the Function and Display the Result
result = remove_punctuations(user_input)
print("String without punctuations:", result)
- Calls the function and prints the result.

Write a python program to reverse a string using user-defined function.

# Function to reverse a string


def reverse_string(input_string):
# Reverse the string using slicing
reversed_string = input_string[::-1]
return reversed_string

# Input string from the user


user_input = input("Enter a string: ")

# Call the function and display the result


result = reverse_string(user_input)
print("Reversed string:", result)

Additional Clarity for the written Program, only for understanding, not for writing:

1. Function Definition
def reverse_string(input_string):
o This function takes a string as input (input_string).
o It will return the reversed version of the input string.
2. Reversing the String
reversed_string = input_string[::-1]
o The slicing technique [::-1] is used to reverse the string.
▪ : selects all characters.
▪ -1 indicates the step to traverse the string in reverse.
3. Returning the Result
return reversed_string
o The reversed string is returned to the caller.
4. Taking User Input
user_input = input("Enter a string: ")
o The program prompts the user to input a string.
5. Calling the Function
result = reverse_string(user_input)
o The input string is passed to the reverse_string function, and the result is
stored in the variable result.
6. Displaying the Result
print("Reversed string:", result)
o The reversed string is printed.

You might also like