Curriculum
Course: Python-100-Programmes-Solutions
Login
Text lesson

Strings-(67 to 79)

Strings

 

67. Count vowels in a string.

Here is a simple Python program to count vowels in a string:

s = input(“Enter a string: “)
count=0
for i in range(0,len(s)):
    if(s[i] ==’a’ or s[i] ==’e’ or s[i] ==’i’ or s[i] ==’o’ or s[i] ==’u’ or s[i] ==’A’ or s[i] ==’E’ or s[i] ==’I’ or s[i] ==’O’ or s[i] ==’U’):
        count += 1
print(“Input string: “, s)
print(“Number of vowels in the string:”, count)
 
or
s = input(“Enter a string: “)
vc=0
for ch in s.lower():
    if ch in “aeiou”:
        vc+=1
             
print(“Input string is : “, s)
print(“The number of vowels in the string: “, vc)
 

 Example

Enter a string: mynetedu 

 Output:

Input string: mynetedu

Number of vowels: 3

 

68. Count vowels & consonants in a string.

 Here is a simple Python program to count vowels and consonants in a string:

s = input(“Enter a string: “)
vc=0
cc=0
for ch in s.lower():
    if ch in “aeiou”:
        vc+=1
    else:
         cc+=1

          
print(“Input string is : “, s)
print(“The number of vowels in the string: “, vc)
print(“The number of Consonent in the string: “, cc)

 

Example

Enter a string: Hello World

 Output:

Input string: Hello World
The number of vowels in the string: 3
The number of Consonants in the string: 8

Number of consonants: 7

 

69. 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: MyNetEdu

 Output:

Number of uppercase letters: 3

70. 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: MyNetEdu

 Output:

Number of lowercase letters: 5

 

71. 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

 

72. 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

 

73. 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.

74. 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: mynetedu

 

Output:

Uppercase string: MYNETEDU

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 the ASCII value back to a character.

  • The difference between lowercase and uppercase letters is 32.

  • ASCII value(A=65 to Z=90 and a=97 to z= 122 and 0=48 to 9=57.

 

75. Convert the string Upper 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 an ASCII value back to a character

  • The difference between uppercase and lowercase letters is 32

76. 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

 

77. 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).

 

78. 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.

79. 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