0% found this document useful (0 votes)
4 views4 pages

PRACTICE QUESTIONS

The document contains a series of Python functions that perform various string manipulations, such as reversing a string, counting characters, checking for palindromes, replacing substrings, and counting vowels and consonants. Each function is demonstrated with an example input string and prints the corresponding output. These functions showcase fundamental programming concepts in Python related to string handling.

Uploaded by

71762305061
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)
4 views4 pages

PRACTICE QUESTIONS

The document contains a series of Python functions that perform various string manipulations, such as reversing a string, counting characters, checking for palindromes, replacing substrings, and counting vowels and consonants. Each function is demonstrated with an example input string and prints the corresponding output. These functions showcase fundamental programming concepts in Python related to string handling.

Uploaded by

71762305061
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/ 4

PRACTICE QUESTIONS:

1)

def reverse_string(s):

return s[::-1]

input_string = "Hello, World!"

print("Reversed string:", reverse_string(input_string))

OUTPUT:

2)

def count_character(s, char):

return s.count(char)

input_string = "Hello, World!"

character = 'o'

print(f"Occurrences of '{character}':", count_character(input_string, character))

OUTPUT:

3) def is_palindrome(s):

return s == s[::-1]

input_string = "madam"

print("Is palindrome:", is_palindrome(input_string))

OUTPUT:

4)
def replace_substring(s, old, new):

return s.replace(old, new)

input_string = "Hello, World!"

print("Replaced string:", replace_substring(input_string, "World", "Python"))

OUTPUT:

5)

def count_vowels_consonants(s):

vowels = "aeiouAEIOU"

vowel_count = sum(1 for char in s if char in vowels)

consonant_count = sum(1 for char in s if char.isalpha() and char not in vowels)

return vowel_count, consonant_count

input_string = "Hello, World!"

vowels, consonants = count_vowels_consonants(input_string)

print("Vowels:", vowels, "Consonants:", consonants)

OUTPUT:

6)

def remove_whitespace(s):

return s.replace(" ", "")

input_string = "Hello, World!"

print("String without whitespace:", remove_whitespace(input_string))

OUTPUT:
7)

def count_words(s):

return len(s.split())

input_string = "Hello, World! How are you?"

print("Number of words:", count_words(input_string))

OUTPUT:

8)

def are_all_digits(s):

return s.isdigit()

input_string = "123456"

print("All characters are digits:", are_all_digits(input_string))

OUTPUT:

9)

def is_alphanumeric(s):

return s.isalnum()

input_string = "Hello123"

print("Is alphanumeric:", is_alphanumeric(input_string))

OUTPUT:

10)

def longest_word(s):

words = s.split()
return max(words, key=len)

input_string = "The quick brown fox jumped over the lazy dog"

print("Longest word:", longest_word(input_string))

OUTPUT:

You might also like