Curriculum
Course: Python-100-Programmes
Login
Text lesson

E-Strings-(67 to 79)

E. Strings

 

  1. Count vowels in a string.

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)

 

Example

Enter a string: Hello World

 Output:

Number of vowels: 3

 

  1. Count consonants in a string.

 

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)

 

Example

Enter a string: Hello World

 Output:

Number of consonants: 7

 

  1. Count uppercase letters in a string.

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)

 

Example

Enter a string: Hello World

 Output:

Number of uppercase letters: 2

  1. Count lowercase letters in a string.

 

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

 

  1. Count digits in a string.

 

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)

 

Example

Enter a string: My phone number is 987654

 

Output:

Number of digits: 6

 

  1. Count spaces in a string.

 

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)

 

Example

Input:

Enter a string: Hello World from Python

 

Output:

Number of spaces: 3

 

  1. Count words in a sentence.

 

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)

 

Example

Input:

Enter a sentence: Python is easy to learn

 

Output:

Number of words: 5

 

How it works

  • 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)

 

Example

Input:

Enter a sentence:  Python is easy to learn

 

Output:

Number of words: 5

 

Explanation (Simple)

  • 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

  1. Convert the string to uppercase.

 

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)

 

Example

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)

 

Example

Input:

Enter a string: Hello World 123

 

Output:

Uppercase string: HELLO WORLD 123

 

Explanation (Simple)

  • ord() gets the ASCII value of a character

  • chr() converts ASCII value back to character

  • Difference between lowercase and uppercase letters is 32

 

  1. Convert the string to lowercase.

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)

 

Example

Input:

Enter a string:  Hello WORLD 123

 

Output:

Lowercase string: hello world 123

 

Explanation (Simple)

  • ord() gives the ASCII value of a character

  • chr() converts ASCII value back to character

  • Difference between uppercase and lowercase letters is 32

  1. Input a sentence and find how many Vowels, Consonants, Uppercase letters, Lowercase letters, Digits, words, and spaces belong to the sentence.

 

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)

 

Example

Input:

Enter a sentence: Hello World 123

 

Output:

Vowels: 3

Consonants: 7

Uppercase letters: 2

Lowercase letters: 8

Digits: 3

Words: 3

Spaces: 2

 

  1. Remove all spaces from a string.

 

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)

 

Example

Input:

Enter a string: Hello World from Python

 

Output:

HelloWorldfromPython

 

Notes

  • Removes only spaces

  • Keeps letters, digits, and symbols unchanged

  • Uses loop + if (beginner-friendly)

 

  1. Find the length of a string without using len().

 

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)

 

Example

Input:

Enter a string:  Hello World

 

Output:

Length of the string: 11

 

Explanation

  • 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))

 

Example

Input:

Enter a string:  Hello World

 

Output:

11

 

Explanation

  • 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

  1. Reverse a string without using slicing.

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)

 

Example

Input:

Enter a string:  Hello World

 

Output:

Reversed string: dlroW olleH

 

Explanation

  • 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)

 

Example

Input:

Hello World

 

Output:

Reversed string: dlroW olleH

 

Explanation

  • s[::-1] slices the string from end to start with step -1

  • Very simple and efficient for reversing strings

 

Scroll to Top