Here is a simple Python program to count vowels in a string:
# Input string
text = input(“Enter a string: “)
# Define vowels
vowels = “aeiouAEIOU”
# Count vowels
count = 0
for ch in text:
if ch in vowels:
count += 1
# Output
print(“Number of vowels:”, count)
Enter a string: Hello World
Output:
Number of vowels: 3
Here is a simple Python program to count consonants in a string:
# Input string
text = input(“Enter a string: “)
# Define vowels
vowels = “aeiouAEIOU”
# Count consonants
count = 0
for ch in text:
if ch.isalpha() and ch not in vowels:
count += 1
# Output
print(“Number of consonants:”, count)
Enter a string: Hello World
Output:
Number of consonants: 7
Here is a simple Python program to count uppercase letters in a string:
# Input string
text = input(“Enter a string: “)
# Count uppercase letters
count = 0
for ch in text:
if ch.isupper():
count += 1
# Output
print(“Number of uppercase letters:”, count)
Enter a string: Hello World
Output:
Number of uppercase letters: 2
Here is a simple Python program to count lowercase letters in a string:
# Input string
text = input(“Enter a string: “)
# Count lowercase letters
count = 0
for ch in text:
if ch.islower():
count += 1
# Output
print(“Number of lowercase letters:”, count)
Example
Enter a string: Hello World
Output:
Number of lowercase letters: 8
Here is a simple Python program to count digits in a string:
# Input string
text = input(“Enter a string: “)
# Count digits
count = 0
for ch in text:
if ch.isdigit():
count += 1
# Output
print(“Number of digits:”, count)
Enter a string: My phone number is 987654
Output:
Number of digits: 6
Here is a simple Python program to count spaces in a string:
# Input string
text = input(“Enter a string: “)
# Count spaces
count = 0
for ch in text:
if ch == ” “:
count += 1
# Output
print(“Number of spaces:”, count)
Input:
Enter a string: Hello World from Python
Output:
Number of spaces: 3
Here is a simple Python program to count words in a sentence:
# Input sentence
sentence = input(“Enter a sentence: “)
# Count words
words = sentence.split()
count = len(words)
# Output
print(“Number of words:”, count)
Input:
Enter a sentence: Python is easy to learn
Output:
Number of words: 5
split() breaks the sentence into words using spaces
len() counts the total number of words
Here is a loop-based Python program to count words in a sentence (no split() used):
# Input sentence
sentence = input(“Enter a sentence: “)
count = 0
in_word = False
for ch in sentence:
if ch != ” ” and not in_word:
count += 1
in_word = True
elif ch == ” “:
in_word = False
# Output
print(“Number of words:”, count)
Input:
Enter a sentence: Python is easy to learn
Output:
Number of words: 5
A word starts when we find a non-space character
in_word helps avoid counting the same word multiple times
Extra spaces do not affect the result
Here is a simple Python program to convert a string to uppercase:
# Input string
text = input(“Enter a string: “)
# Convert to uppercase
upper_text = text.upper()
# Output
print(“Uppercase string:”, upper_text)
Input:
Enter a string: Hello World
Output:
Uppercase string: HELLO WORLD
Here is a loop-based Python program to convert a string to uppercase (without using upper()):
# Input string
text = input(“Enter a string: “)
result = “”
for ch in text:
if ‘a’ <= ch <= ‘z’:
result += chr(ord(ch) – 32)
else:
result += ch
# Output
print(“Uppercase string:”, result)
Input:
Enter a string: Hello World 123
Output:
Uppercase string: HELLO WORLD 123
ord() gets the ASCII value of a character
chr() converts ASCII value back to character
Difference between lowercase and uppercase letters is 32
Here is a loop-based Python program to convert a string to lowercase (without using lower()):
# Input string
text = input(“Enter a string: “)
result = “”
for ch in text:
if ‘A’ <= ch <= ‘Z’:
result += chr(ord(ch) + 32)
else:
result += ch
# Output
print(“Lowercase string:”, result)
Input:
Enter a string: Hello WORLD 123
Output:
Lowercase string: hello world 123
ord() gives the ASCII value of a character
chr() converts ASCII value back to character
Difference between uppercase and lowercase letters is 32
Here is a single Python program that inputs a sentence and counts
Vowels, Consonants, Uppercase letters, Lowercase letters, Digits, Words, and Spaces
(beginner-friendly):
s = input(“Enter a sentence: “)
vowels = 0
consonants = 0
uppercase = 0
lowercase = 0
digits = 0
spaces = 0
words = 0
in_word = False
for ch in s:
# Uppercase
if ‘A’ <= ch <= ‘Z’:
uppercase += 1
if ch in “AEIOU”:
vowels += 1
else:
consonants += 1
if not in_word:
words += 1
in_word = True
# Lowercase
elif ‘a’ <= ch <= ‘z’:
lowercase += 1
if ch in “aeiou”:
vowels += 1
else:
consonants += 1
if not in_word:
words += 1
in_word = True
# Digit
elif ‘0’ <= ch <= ‘9’:
digits += 1
in_word = False
# Space
elif ch == ” “:
spaces += 1
in_word = False
else:
in_word = False
print(“Vowels:”, vowels)
print(“Consonants:”, consonants)
print(“Uppercase letters:”, uppercase)
print(“Lowercase letters:”, lowercase)
print(“Digits:”, digits)
print(“Words:”, words)
print(“Spaces:”, spaces)
Input:
Enter a sentence: Hello World 123
Output:
Vowels: 3
Consonants: 7
Uppercase letters: 2
Lowercase letters: 8
Digits: 3
Words: 3
Spaces: 2
Here is a very simple Python program to remove all spaces from a string:
s = input(“Enter a string: “)
result = “”
for ch in s:
if ch != ” “:
result = result + ch
print(result)
Input:
Enter a string: Hello World from Python
Output:
HelloWorldfromPython
Removes only spaces
Keeps letters, digits, and symbols unchanged
Uses loop + if (beginner-friendly)
Here is a simple Python program to find the length of a string without using len():
# Input string
s = input(“Enter a string: “)
count = 0
for ch in s:
count += 1
print(“Length of the string:”, count)
Input:
Enter a string: Hello World
Output:
Length of the string: 11
Initialize a counter (count = 0)
Loop through each character in the string
Increase the counter by 1 for each character
The final value of count is the length of the string
Here is a one-line Python version to find the length of a string using a loop:
s = input(“Enter a string: “)
print(sum(1 for _ in s))
Input:
Enter a string: Hello World
Output:
11
for _ in s loops through each character in the string
sum(1 for _ in s) adds 1 for each character
Result is the length of the string
Here is a Python program to reverse a string without using slicing:
# Input string
s = input(“Enter a string: “)
reversed_str = “”
for ch in s:
reversed_str = ch + reversed_str # Add each character at the beginning
print(“Reversed string:”, reversed_str)
Input:
Enter a string: Hello World
Output:
Reversed string: dlroW olleH
Start with an empty string reversed_str
For each character ch in the string, prepend it to reversed_str
At the end, reversed_str contains the original string in reverse
Here is a Python program to reverse a string using slicing:
# Input string
s = input(“Enter a string: “)
# Reverse using slicing
reversed_str = s[::-1]
print(“Reversed string:”, reversed_str)
Input:
Hello World
Output:
Reversed string: dlroW olleH
s[::-1] slices the string from end to start with step -1
Very simple and efficient for reversing strings