Practice Corner Handling Strings

Handling Strings

Strings are a fundamental data type in Python. They allow us to work with textual data and provide a wide range of operations and methods to manipulate and analyze text. This page includes beginner-friendly exercises on string handling, combining logic building using loops and conditions, as well as string functions.

Practice Problems

Problems Index

  1. Input a string and count the number of vowels in it.
  2. Input a string and reverse it using a loop.
  3. Input a string and check if it is a palindrome.
  4. Input two strings and check if they are anagrams.
  5. Input a string and remove all spaces from it.
  6. Input a string and find the frequency of each character.
  7. Input a sentence and count the number of words.
  8. Input a string and convert it to uppercase without using str.upper().
  9. Input a string and print each character along with its ASCII value.
  10. Input a string and replace all vowels with *.

Try It Yourself

Now that you’ve reviewed the problems, practice them below using the code runner!

Pyground

Play with Python!

Output:

Solutions

Solutions

  1. Input a string and count the number of vowels in it.

    Show Code
    string = input("Enter a string: ")
    vowels = "aeiouAEIOU"
    count = 0
    for char in string:
        if char in vowels:
            count += 1
    print("Number of vowels:", count)
  2. Input a string and reverse it using a loop.

    Show Code
    string = input("Enter a string: ")
    reversed_string = ""
    for char in string:
        reversed_string = char + reversed_string
    print("Reversed string:", reversed_string)
  3. Input a string and check if it is a palindrome.

    Show Code
    string = input("Enter a string: ")
    is_palindrome = True
    for i in range(len(string) // 2):
        if string[i] != string[-(i + 1)]:
            is_palindrome = False
            break
    if is_palindrome:
        print("The string is a palindrome.")
    else:
        print("The string is not a palindrome.")
  4. Input two strings and check if they are anagrams.

    Show Code
    str1 = input("Enter the first string: ")
    str2 = input("Enter the second string: ")
    if sorted(str1) == sorted(str2):
        print("The strings are anagrams.")
    else:
        print("The strings are not anagrams.")
  5. Input a string and remove all spaces from it.

    Show Code
    string = input("Enter a string: ")
    result = ""
    for char in string:
        if char != " ":
            result += char
    print("String without spaces:", result)
  6. Input a string and find the frequency of each character.

    Show Code
    string = input("Enter a string: ")
    frequency = {}
    for char in string:
        if char in frequency:
            frequency[char] += 1
        else:
            frequency[char] = 1
    print("Character frequencies:", frequency)
  7. Input a sentence and count the number of words.

    Show Code
    sentence = input("Enter a sentence: ")
    word_count = 1
    for char in sentence:
        if char == " ":
            word_count += 1
    print("Number of words:", word_count)
  8. Input a string and convert it to uppercase without using str.upper().

    Show Code
    string = input("Enter a string: ")
    uppercase_string = ""
    for char in string:
        if 'a' <= char <= 'z':
            uppercase_string += chr(ord(char) - 32)
        else:
            uppercase_string += char
    print("Uppercase string:", uppercase_string)
  9. Input a string and print each character along with its ASCII value.

    Show Code
    string = input("Enter a string: ")
    for char in string:
        print(f"Character: {char}, ASCII value: {ord(char)}")
  10. Input a string and replace all vowels with *.

    Show Code
    string = input("Enter a string: ")
    vowels = "aeiouAEIOU"
    result = ""
    for char in string:
        if char in vowels:
            result += "*"
        else:
            result += char
    print("String after replacing vowels:", result)