Tuesday 17 August 2021

Program to count total number of characters in a given String

Following Python program will count total number of characters in a given string

Expected Output: If user enters string = Hello Students, output should be

Total Number of Characters in a String = 14

Program Code:

# Python program to count total number of characters in a given String
 
str1 = input("Enter your string: ")
count = 0  #initialize counter to 0
 
for i in str1:
    count = count + 1
 
print("Total Number of Characters in a String = ", count)

Output of Program:

Enter your string: Hello Students Total Number of Characters in a String = 14


* * * * *

< Back to Python Loops >


Saturday 14 August 2021

Square pattern of star (*) using for loop

Following Python program will print square pattern of star (*) using for loop. Number of lines to be printed are entered by the user

Expected Output: If user enters lines = 3, output should be

* * * * * * * * *

Program Code:

lines = int(input("Enter number of lines:"))

for i in range(lines):    
   for i in range(lines):        
      print('*', end = '  ')    
   print()

Output of Program:

Enter number of lines:3 * * * * * * * * *


* * * * *


< Back to Python Loops >





Monday 9 August 2021

Even numbers between 1 to 10 using for loop

Following Python program will print even numbers between 1 to 10 using for loop. 

Expected Output: 2 4 6 8 10 


Program Code:

for i in range(2, 11, 2):
  print (i, end=" ")

Output of Program:

2 4 6 8 10


* * * * *


< Back to Python Loops >





Sunday 8 August 2021

Odd numbers between 1 to 10 using while loop

Following Python program will print odd numbers between 1 to 10 using while loop. 

Expected Output: 1 3 5 7 9 


Program Code:

number = 1

while number <= 10:
  print(number, end=" ")
  number = number + 2

Output of Program:

1 3 5 7 9


* * * * *


< Back to Python Loops >

< Back to Home >