Python String Practice Problems: Exercises with Solutions

In the previous section we discussed the basics of Python Strings. Now let us take a look at some real-life application Problems.

 

1. Reverse a String

Write a program to reverse a given string without using built-in reverse functions.

				
					s = input("Enter a string: ")
rev = ""

for ch in s:
    rev = ch + rev

print("Reversed string:", rev)

                   
Enter a string: qubrica
Reversed string: acirbuq

				
			
IterationCharacter (ch)Operation (ch + rev)Value of rev
Start---
1'q''q' + " "q
2'u''u' + "q"uq
3'b''b' + "uq"buq
4'r''r' + "buq"rbuq
5'i''i' + "rbuq"irbuq
6'c''c' + "irbuq"cirbuq
7'a''a' + "cirbuq"acirbuq

2. String Concatenation

Write a Python Program to concatenate two Strings.

				
					s1 = input("Enter first string: ")
s2 = input("Enter second string: ")

result = s1 + s2

print("Concatenated String:", result)

                   
# Enter first string: python
# Enter second string: programming
# Concatenated String: pythonprogramming
				
			

3. String Length

Write a Python Program to find the string length without using a built in function.

				
					s = input("Enter a string: ")
count = 0

for ch in s:
    count = count + 1
print("Length of the string is:", count)

# Enter a string: qubrica
# Length of the string is: 7

				
			

4. Largest Word in a Sentence

Write a Program to find Largest word in a Sentence 

				
					s = input("Enter a sentence: ")
words = s.split()

longest = words[0]

for word in words:
    if len(word) > len(longest):
        longest = word

print("Longest word:", longest)

# Enter a sentence: python programming is easy
# Longest word: programming

				
			

5. Convert Uppercase to Lowercase (Without built-in function)

Write a Python Program to convert Uppercase letters to Lowercase

				
					s = input("Enter a string: ")
result = ""

for ch in s:
    if 'A' <= ch <= 'Z':
        result += chr(ord(ch) + 32)
    else:
        result += ch

print("Converted string:", result)

# Enter a string: PYTHON
# Converted string: python

				
			

6. Anagram

Write a program to check if two strings are anagrams or not.

(An anagram is a word formed by rearranging the letters of a different word)

listen = silent

				
					s1 = input("Enter first string: ")
s2 = input("Enter second string: ")

if sorted(s1) == sorted(s2):
    print("Anagram")
else:
    print("Not Anagram")

# Enter first string: listen
# Enter second string: silent
# Anagram

				
			

7. Count Digits, Letters and Special Characters

Write a Python Program to count how many digits, alphabets and special characters exist.

				
					s = input("Enter a string: ")

letters = digits = special = 0

for ch in s:
    if ch.isalpha():
        letters += 1
    elif ch.isdigit():
        digits += 1
    else:
        special += 1

print("Letters:", letters)
print("Digits:", digits)
print("Special Characters:", special)

# Enter a string: strings123@
# Letters: 7
# Digits: 3
# Special Characters: 1

				
			

8. Remove Space from String

Write a C program to remove all spaces from the string.

				
					s = input("Enter a string: ")
result = ""

for ch in s:
    if ch != " ":
        result += ch

print("String without spaces:", result)

# Enter a string: Python Programming
# String without spaces: PythonProgramming

				
			

9. Count Vowels and Consonants

Write a Python Program to count the number of vowels and consonants in a string.

				
					s = input("Enter a string: ").lower()

vowels = "aeiou"
v = c = 0

for ch in s:
    if ch.isalpha():
        if ch in vowels:
            v += 1
        else:
            c += 1

print("Vowels:", v)
print("Consonants:", c)

# Enter a string: Aeroplane
# Vowels: 5
# Consonants: 4