Python Strings Practice Questions with Solutions

Strings are one of the most commonly used data types in Python. Python Strings Practice Questions with Solutions are used to store and manipulate text. In this practice set, you’ll learn how to create strings, access characters, slice strings, and use common string methods through beginner-friendly Python programs.


1. Python Program to Calculate the Length of a String

Problem Statement

Write a Python program to find the length of a string entered by the user.

Python Solution

text = input("Enter a string: ")

print("Length:", len(text))

Sample Output

Enter a string: CodeMantra
Length: 11

Explanation

The len() function returns the total number of characters in a string.

Concepts Covered

  • len()
  • String Length

2. Python Program to Convert a String to Uppercase and Lowercase

Problem Statement

Write a Python program to convert a string into uppercase and lowercase.

Python Solution

text = input("Enter a string: ")

print("Uppercase:", text.upper())
print("Lowercase:", text.lower())

Sample Output

Enter a string: Python
Uppercase: PYTHON
Lowercase: python

Explanation

The upper() method converts all characters to uppercase, while lower() converts them to lowercase.

Concepts Covered

  • upper()
  • lower()

3. Python Program to Reverse a String

Difficulty: Foundation

Problem Statement

Write a Python program to reverse a string entered by the user.

Python Solution

text = input("Enter a string: ")

print("Reversed String:", text[::-1])

Sample Output

Enter a string: Python
Reversed String: nohtyP

Explanation

String slicing with [::-1] returns the string in reverse order.

Concepts Covered

  • String Slicing
  • Reverse String

4. Python Program to Check Whether a String is a Palindrome

Problem Statement

Write a Python program to check whether a string is a palindrome.

Python Solution

text = input("Enter a string: ")

if text == text[::-1]:
    print("Palindrome")
else:
    print("Not a Palindrome")

Sample Output

Enter a string: madam
Palindrome

Explanation

A palindrome reads the same from left to right and right to left.

Concepts Covered

  • String Comparison
  • String Slicing

5. Python Program to Count Vowels in a String

Problem Statement

Write a Python program to count the total number of vowels in a string.

Python Solution

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

count = 0

for char in text:
    if char in "aeiou":
        count += 1

print("Total Vowels:", count)

Sample Output

Enter a string: CodeMantra
Total Vowels: 4

Explanation

The program checks each character and counts the vowels.

Concepts Covered

  • for loop
  • Membership operator
  • String Traversal

6. Python Program to Count Words in a Sentence

Problem Statement

Write a Python program to count the total number of words in a sentence.

Python Solution

sentence = input("Enter a sentence: ")

words = sentence.split()

print("Total Words:", len(words))

Sample Output

Enter a sentence: Learn Python with CodeMantra
Total Words: 4

Explanation

The split() method divides the sentence into words.

Concepts Covered

  • split()
  • len()

7. Python Program to Replace a Word in a String

Problem Statement

Write a Python program to replace one word with another.

Python Solution

text = input("Enter a sentence: ")

new_text = text.replace("Python", "Java")

print("Updated String:", new_text)

Sample Output

Enter a sentence: I am learning Python
Updated String: I am learning Java

Explanation

The replace() method replaces the specified word with another word.

Concepts Covered

  • replace()

8. Python Program to Check Whether a Character Exists in a String

Problem Statement

Write a Python program to check whether a character exists in a string.

Python Solution

text = input("Enter a string: ")
character = input("Enter a character: ")

if character in text:
    print("Character Found")
else:
    print("Character Not Found")

Sample Output

Enter a string: Python
Enter a character: t
Character Found

Explanation

The in operator checks whether a character exists in a string.

Concepts Covered

  • in operator
  • String Search

9. Python Program to Remove All Spaces from a String

Problem Statement

Write a Python program to remove all spaces from a string.

Python Solution

text = input("Enter a string: ")

text = text.replace(" ", "")

print("Updated String:", text)

Sample Output

Enter a string: Learn Python Programming
Updated String: LearnPythonProgramming

Explanation

The replace() method replaces every space with an empty string.

Concepts Covered

  • String Methods
  • replace()

10. Python Program to Count the Occurrences of a Character

Problem Statement

Write a Python program to count how many times a character appears in a string.

Python Solution

text = input("Enter a string: ")
character = input("Enter a character: ")

count = text.count(character)

print("Occurrences:", count)

Sample Output

Enter a string: programming
Enter a character: g
Occurrences: 2

Explanation

The count() method returns the total number of occurrences of a specified character or substring.

Concepts Covered

  • count()
  • String Methods

11. Python Program to Count the Frequency of Each Character in a String

Problem Statement

Write a Python program to count the frequency of each character in a given string.

Python Solution

text = input("Enter a string: ")

frequency = {}

for char in text:
    if char in frequency:
        frequency[char] += 1
    else:
        frequency[char] = 1

print("Character Frequency:")

for key, value in frequency.items():
    print(key, ":", value)

Sample Output

Enter a string: python

Character Frequency:
p : 1
y : 1
t : 1
h : 1
o : 1
n : 1

Explanation

The program stores every character as a dictionary key and increases its count whenever the character appears again.

Concepts Covered

  • Strings
  • Dictionary
  • for Loop
  • Character Frequency

12. Python Program to Check Whether Two Strings are Anagrams

Problem Statement

Write a Python program to check whether two strings are anagrams of each other.

Python Solution

string1 = input("Enter first string: ").lower()
string2 = input("Enter second string: ").lower()

if sorted(string1) == sorted(string2):
    print("The strings are Anagrams.")
else:
    print("The strings are Not Anagrams.")

Sample Output

Enter first string: listen
Enter second string: silent

The strings are Anagrams.

Explanation

Two strings are anagrams if they contain the same characters with the same frequency, regardless of their order.

Concepts Covered

  • sorted()
  • String Comparison
  • User Input

13. Python Program to Find the Longest Word in a Sentence

Problem Statement

Write a Python program to find the longest word in a sentence entered by the user.

Python Solution

sentence = input("Enter a sentence: ")

words = sentence.split()

longest = max(words, key=len)

print("Longest Word:", longest)

Sample Output

Enter a sentence: Python programming is very interesting

Longest Word: programming

Explanation

The split() method converts the sentence into a list of words, and max() with key=len returns the longest word.

Concepts Covered

  • split()
  • max()
  • len()
  • Lists

14. Python Program to Count Vowels, Consonants, Digits, and Special Characters

Problem Statement

Write a Python program to count vowels, consonants, digits, and special characters in a string.

Python Solution

text = input("Enter a string: ")

vowels = consonants = digits = special = 0

for ch in text:
    if ch.lower() in "aeiou":
        vowels += 1
    elif ch.isalpha():
        consonants += 1
    elif ch.isdigit():
        digits += 1
    else:
        special += 1

print("Vowels:", vowels)
print("Consonants:", consonants)
print("Digits:", digits)
print("Special Characters:", special)

Sample Output

Enter a string: Python123!

Vowels: 1
Consonants: 5
Digits: 3
Special Characters: 1

Explanation

The program checks every character using built-in string methods like isalpha() and isdigit() to classify it.

Concepts Covered

  • Character Classification
  • String Methods
  • for Loop
  • Conditional Statements

15. Python Program to Find the First Non-Repeating Character in a String

Problem Statement

Write a Python program to find the first non-repeating character in a string.

Python Solution

text = input("Enter a string: ")

for ch in text:
    if text.count(ch) == 1:
        print("First Non-Repeating Character:", ch)
        break
else:
    print("No Non-Repeating Character Found")

Sample Output

Enter a string: aabbcdde

First Non-Repeating Character: c

Explanation

The program checks each character and uses the count() method to determine whether it appears only once.

Concepts Covered

  • count()
  • for Loop
  • break Statement
  • String Traversal

Frequently Asked Questions (FAQs)

1. What are strings in Python?

A string is a sequence of characters enclosed in single quotes (' '), double quotes (" "), or triple quotes (''' ''' or """ """). Strings are used to store and manipulate textual data.

Example:

name = "Python"

2. Are Python strings mutable?

No. Python strings are immutable, which means their contents cannot be changed after they are created. Any modification creates a new string instead of changing the original one.

Example:

text = "Python"

# Creates a new string
text = text.replace("Python", "Java")

print(text)

3. What are the most commonly used string methods in Python?

Some commonly used string methods are:

  • upper()
  • lower()
  • title()
  • capitalize()
  • strip()
  • replace()
  • find()
  • count()
  • split()
  • join()
  • startswith()
  • endswith()

These methods simplify string manipulation tasks.


4. How do I access characters in a string?

You can access characters using indexing. Python uses zero-based indexing.

Example:

text = "Python"

print(text[0])   # P
print(text[3])   # h
print(text[-1])  # n

5. Why are strings important in Python?

Strings are one of the most frequently used data types in Python. They are essential for handling user input, file processing, web development, data analysis, automation, APIs, natural language processing (NLP), and machine learning. Learning string operations is fundamental for becoming a proficient Python developer.

Chapter Summary

After completing this chapter, you have learned:

  • Creating and working with strings
  • len()
  • upper() and lower()
  • String slicing
  • Reversing strings
  • Checking palindromes
  • Counting vowels
  • Counting words
  • Replacing text
  • Searching within strings
  • Removing spaces
  • Counting character occurrences

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top