JitCoder

Python String Programs for Practice (With Output) – 25+ Examples

Strings are one of the most important data types in Python. Whether you are learning Python for programming, data science, automation, or interview preparation, mastering string manipulation is essential.

In this guide, you’ll learn Python String Programs for Practice with detailed explanations and outputs. These programs help beginners understand string operations while improving problem-solving skills.

By the end of this tutorial, you will be able to solve common Python string questions asked in coding interviews and exams.


Table of contents

Table of Contents

What Is a String in Python?

A string is a sequence of characters enclosed in single quotes (' '), double quotes (" "), or triple quotes (''' ''').

Example

name = "Python"print(name)

Output

Python

Strings are immutable, meaning their contents cannot be changed after creation.


Why Practice Python String Programs?

Practicing Python string programs helps you:

  • Understand string manipulation
  • Improve coding logic
  • Prepare for technical interviews
  • Learn built-in string methods
  • Build a strong programming foundation

1. Program to Find Length of a String

Code

text = "Python Programming"length = len(text)print("Length:", length)

Output

Length: 18

Explanation

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


2. Program to Reverse a String

Code

text = "Python"reverse = text[::-1]print(reverse)

Output

nohtyP

Explanation

String slicing with [::-1] reverses the string.


3. Program to Check Palindrome String

A palindrome reads the same forward and backward.

Code

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

Output

Palindrome

4. Program to Count Vowels in a String

Code

text = "Python Programming"count = 0for ch in text.lower():    if ch in "aeiou":        count += 1print("Vowels:", count)

Output

Vowels: 4

5. Program to Count Consonants

Code

text = "Python"count = 0for ch in text.lower():    if ch.isalpha() and ch not in "aeiou":        count += 1print("Consonants:", count)

Output

Consonants: 4

6. Program to Convert String to Uppercase

Code

text = "python programming"print(text.upper())

Output

PYTHON PROGRAMMING

7. Program to Convert String to Lowercase

Code

text = "PYTHON"print(text.lower())

Output

python

8. Program to Count Words in a String

Code

text = "Python is easy to learn"words = len(text.split())print(words)

Output

5

9. Program to Remove Spaces from String

Code

text = "Python Programming"result = text.replace(" ", "")print(result)

Output

PythonProgramming

10. Program to Check Whether a Character Exists

Code

text = "Python"char = "t"if char in text:    print("Found")else:    print("Not Found")

Output

Found

11. Program to Count Occurrences of a Character

Code

text = "programming"print(text.count("m"))

Output

2

12. Program to Find First Occurrence of Character

Code

text = "python"print(text.find("t"))

Output

2

13. Program to Replace a Character in String

Code

text = "Hello World"result = text.replace("World", "Python")print(result)

Output

Hello Python

14. Program to Check Anagram Strings

Two strings are anagrams if they contain the same characters.

Code

str1 = "listen"str2 = "silent"if sorted(str1) == sorted(str2):    print("Anagram")else:    print("Not Anagram")

Output

Anagram

15. Program to Find Duplicate Characters

Code

text = "programming"duplicates = []for ch in text:    if text.count(ch) > 1 and ch not in duplicates:        duplicates.append(ch)print(duplicates)

Output

['r', 'g', 'm']

16. Program to Remove Duplicate Characters

Code

text = "programming"result = ""for ch in text:    if ch not in result:        result += chprint(result)

Output

progamin

17. Program to Find Frequency of Each Character

Code

text = "hello"for ch in set(text):    print(ch, ":", text.count(ch))

Output

h : 1e : 1l : 2o : 1

18. Program to Check String Starts With a Specific Word

Code

text = "Python Programming"print(text.startswith("Python"))

Output

True

19. Program to Check String Ends With a Specific Word

Code

text = "python.py"print(text.endswith(".py"))

Output

True

20. Program to Capitalize First Letter

Code

text = "python programming"print(text.capitalize())

Output

Python programming

21. Program to Count Digits in a String

Code

text = "Python12345"count = 0for ch in text:    if ch.isdigit():        count += 1print(count)

Output

5

22. Program to Separate Alphabets and Numbers

Code

text = "Python123"letters = ""numbers = ""for ch in text:    if ch.isalpha():        letters += ch    elif ch.isdigit():        numbers += chprint("Letters:", letters)print("Numbers:", numbers)

Output

Letters: PythonNumbers: 123

23. Program to Find ASCII Value of Characters

Code

text = "ABC"for ch in text:    print(ch, ord(ch))

Output

A 65B 66C 67

24. Program to Convert First Letter of Every Word to Uppercase

Code

text = "python string programs"print(text.title())

Output

Python String Programs

25. Program to Find the Longest Word in a Sentence

Code

text = "Python programming language is powerful"words = text.split()longest = max(words, key=len)print(longest)

Output

programming

26. Program to Count Special Characters

Code

text = "Python@123#"count = 0for ch in text:    if not ch.isalnum():        count += 1print(count)

Output

2

27. Program to Remove All Special Characters

Code

text = "Python@123#"result = ""for ch in text:    if ch.isalnum():        result += chprint(result)

Output

Python123

Common Python String Methods

MethodDescription
len()Returns string length
upper()Converts to uppercase
lower()Converts to lowercase
replace()Replaces text
split()Splits string
join()Joins strings
strip()Removes spaces
find()Finds position
count()Counts occurrences
startswith()Checks prefix
endswith()Checks suffix
title()Capitalizes each word

Python String Programs Interview Questions for Practice

Here are some commonly asked interview questions:

  1. Reverse a string without slicing.
  2. Check if a string is palindrome.
  3. Count vowels and consonants.
  4. Find duplicate characters.
  5. Check anagram strings.
  6. Remove duplicate characters.
  7. Find frequency of each character.
  8. Count words in a sentence.
  9. Find longest word.
  10. Remove special characters.

Practicing these questions will improve your Python programming skills significantly.


Best Tips to Master Python String Programs

  • Learn string indexing and slicing.
  • Practice one string problem daily.
  • Use built-in string methods.
  • Understand loops and conditions.
  • Solve coding challenges regularly.
  • Experiment with real-world text data.
  • Build mini projects involving text processing.

Conclusion

Learning Python String Programs for Practice is one of the fastest ways to strengthen your Python fundamentals. Strings are used in almost every Python application, from web development and automation to data science and machine learning.

The 27 examples covered in this guide include basic, intermediate, and interview-level string problems with outputs. Practice these programs repeatedly, understand the logic behind each solution, and try creating your own variations.

By mastering Python string operations, you’ll build a solid foundation for advanced Python topics and technical interviews.

Python Functions Explained

Python Exception Handling

Python File Handling

Python Recursion Explained

NumPy Basics Explained

Useful Resources for Learning Python Loops

If you want to explore Python loops in greater depth, these official resources are highly recommended:

Leave a Comment

Your email address will not be published. Required fields are marked *