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
- Input a string and count the number of vowels in it.
- Input a string and reverse it using a loop.
- Input a string and check if it is a palindrome.
- Input two strings and check if they are anagrams.
- Input a string and remove all spaces from it.
- Input a string and find the frequency of each character.
- Input a sentence and count the number of words.
- Input a string and convert it to uppercase without using
str.upper()
. - Input a string and print each character along with its ASCII value.
- 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
-
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)
-
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)
-
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.")
-
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.")
-
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)
-
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)
-
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)
-
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)
-
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)}")
-
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)