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

Difficulty: Foundation

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

Difficulty: Foundation

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

Difficulty: Foundation

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

Difficulty: Practice

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

Difficulty: Practice

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

Difficulty: Practice

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

Difficulty: Challenge

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

Difficulty: Challenge

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

Difficulty: Challenge

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

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