Find the total number of special characters

In this program, we will find the total number of special characters in a file.

Steps to solve the program
  1. Create a variable to count the total number of special characters and assign its value equal to 0.
  2. Open the desired file using open() in reading mode.
  3. Use for loop iterate over the data.
  4. If the character is a special character then add 1 to the variable that we have created.
  5. Use isalnum() for this purpose.
  6. Print the output.
				
					f1=open("file1.txt","r")
data=f1.read()

is_special = 0

for char in data:
    if char.isalnum():
        is_special += 1
        
print("NUmber of digits: ",is_special)
				
			

Output :

				
					3
				
			

find the total number of Uppercase letters in a file

sort a list using for loop

Program to find the total number of Uppercase letters

In this program, we will find the total number of Uppercase letters in a file.

Steps to solve the program
  1. Create a variable to count the total number of uppercase letters and assign its value equal to 0.
  2. Open the desired file using open() in reading mode.
  3. Use for loop iterate over the data.
  4. If the character is an Uppercase letter then add 1 to the variable that we have created.
  5. Use isupper() for this purpose.
  6. Print the output.
				
					f1=open("file1.txt","r")
data=f1.read()

is_upper = 0

for char in data:
    if char.isupper():
        is_upper += 1
        
print("NUmber of digits: ",is_upper)
				
			

Output :

				
					2
				
			

find the total number of digits in a file

find the total number of special characters in a file

Find the total number of digits in a file

In this program, we will count the the total number of digits in a file.

Steps to solve the program
  1. Create a variable to count the total number of digits and assign its value equal to 0.
  2. Open the desired file using open() in reading mode.
  3. Use for loop iterate over the data.
  4. If the element is a digit then add 1 to the variable that we have created.
  5. Use isdigit() for this purpose.
  6. Print the output.
				
					f1=open("file1.txt","r")
data=f1.read()

is_digit = 0

for i in data:
    if i.isdigit():
        is_digit+=1
        
print("NUmber of digits: ",is_digit)
				
			

Output :

				
					3
				
			

count the total number of characters in a file

find the total number of Uppercase letters in a file

Count the total number of characters in a file

In this program, we will count the total number of characters in a file

Steps to solve the program
  1. Create a variable to count the total number of characters and assign its value equal to 0.
  2. Open the desired file using open() in reading mode.
  3. Use for loop iterate over the data.
  4. If the element is character then add 1 to the variable that we have created.
  5. Use isalpha() for this purpose.
  6. Print the output.
				
					char=0

f=open("file2.txt","r")
f1=f.read()
for ele in f1:
    if ele.isalpha():
        char+=1
f.close()

print("Number of characters: ",char)
				
			

Output :

				
					26
				
			

print 1-10 natural numbers but it should stop when the number is 6

find the total number of digits in a file

Print the numbers by breaking a loop in Python.

In this program, we will print the numbers by breaking a loop in Python.

Steps to solve the program
  1. Use for loop to iterate over numbers from 1-10.
  2. If a number is not equal to 6 print the number.
  3. If a number is equal to 6 break the loop.
				
					
for i in range(1,11):
    if i != 6:
        print(i)
    if i == 6:
        break
				
			

Output :

				
					1
2
3
4
5
				
			

print 1-10 natural numbers but it should stop when the number is 6

count the total number of characters in a file

Python Program To Print Numbers Before A Number.

In this program, we will print the numbers before a number.

Steps to solve the program
  1. Create a variable and assign its value equal to 1.
  2. While the value of the variable is less than 11 print the number.
  3. Add 1 to the variable.
  4. If the value of the variable is equal to 6 break the loop.
				
					#81
count = 1

while count < 11:
    print(count)
    count += 1
    if count == 6:
        break
				
			

Output :

				
					1
2
3
4
5
				
			

print the last element of a list using a while loop

print 1-10 natural numbers but it should stop when the number is 6

The last element of list using a while loop

In this program, we will print the last element of list using a while loop.

Steps to solve the program
  1. Take a list as input, create a variable and assign its value equal to 0.
  2. While the value of the variable is less than the length of the list add 1 to the variable.
  3. At the end of the loop, the variable’s value is equal to the length of the list.
  4. Print the last element of the list with the help of that variable and indexing.
				
					list1 = ["s","q","a","t","o","o","l","s"]
count = 0

while count<len(list1):
    count += 1
    a = count

print("Last element of the list: ",list1[a-1])
				
			
				
					Last element of the list:  s
				
			

Output :

add special characters in an empty list from a given list

print 1-10 natural numbers but it should stop when the number is 6

Add special characters in an empty list

In this program, we will add special characters to an empty list.

Steps to solve the program
  1. Take a list as input.
  2. Create a list of special characters and create an empty list.
  3. Use for loop to iterate over input list.
  4. If the character from the input list is in the list of special characters then add it to the empty list.
  5. Print the output.
				
					list1 = ["s",2,4,6,"a","@","!","%","#"]
special_char = ["!","@","#","$","%","^","}"
               ,"&","*","(",")","{","[","]"]
list2 = []

for char in list1:
    if char in special_char:
        list2.append(char)
        
print(list2)
				
			

Output :

				
					['@', '!', '%', '#']
				
			

add odd numbers in an empty list from a given list

print the last element of a list using a while loop

Program to add odd numbers in an empty list

In this program, we will add odd numbers to an empty list.

Steps to solve the program
  1. Take a list as input and create an empty list.
  2. Use for loop to iterate over each number of the input list.
  3. If the number is odd then add it to the empty list.
  4. Print the output.
				
					list1 = [3,8,5,0,2,7]
odd = []

for val in list1:
    if val%2 != 0:
        odd.append(val)
        
print("Even numbers: ",odd)
				
			

Output :

				
					Even numbers:  [3, 5, 7]
				
			

add even numbers in an empty list from a given list

add special characters in an empty list from a given list 

Program to add even numbers in an empty list

In this program, we will add even numbers to an empty list.

Steps to solve the program
  1. Take a list as input and create an empty list.
  2. Use for loop to iterate over each number of the input list.
  3. If the number is even then add it to the empty list.
  4. Print the output.
				
					list1 = [2,3,5,76,9,0,16]
even = []

for val in list1:
    if val%2 == 0:
        even.append(val)
        
print("Even numbers: ",even)
				
			

Output :

				
					Even numbers:  [2, 76, 0, 16]
				
			

find the total number of special characters in a string

add odd numbers in an empty list from a given list