Program to remove specific line in a file and write it to another file

In this python file program, we will remove specific line in a file and write it to another file with the help of the below-given steps. Let’s consider we have readcontent.txt file with the below content.

				
					#readcontent.txt
My name is john.
I'm 20 years old.
I'm learning python at sqatools.
I'm from Australia.

				
			
Steps to solve the program
  1. Open the first file in the reading mode using open(‘readcontent.txt’,’r’).
  2. Open the second file in the append mode using open(‘writecontent.txt’,’a’).
  3. Read the lines of the file using readlines().
  4. Use a for loop to iterate over the lines.
  5. Using an if statement to check whether ‘t’ is in the line or not.
  6. If yes then add that line to the second file using write().
  7. Now close both files.
				
					# open file read mode
f1=open('readcontent.txt','r')
# open file in append mode
f2=open('writecontent.txt','a')
# Read lines from the file
lines=f1.readlines()
# Iterate over lines
for line in lines:
# Check for t in the lines
    if 't' in line:
    # Write lines to writecontent.txt file
        f2.write(line)
# Close the file
f1.close()
f2.close()
				
			

Output : Open writecontent.txt file to see the output.

I’m learning python at sqatools.
I’m from Australia.

 

 

count the total number of consonants in a file.

display words from a file that has less than 5 characters.

Program to get all permutations of a string using a string

In this python function program, we will get all permutations of a string using a string.

Permutation:
A permutation is an arrangement of objects in a definite order. It is a way of changing or arranging the elements or objects in a linear order.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Import itertools
  2. Create a function permutations
  3. Use the def keyword to define the function.
  4. Take a string as input through the user.
  5. Convert the string to the list using list().
  6. Find the permutaions i.e. different possible combinations of characters in the list using itertools.permutations(list1).
  7. Convert the result back to the list using the list.
    return the output.
  8. Call the function to get the output.
				
					import itertools
def permutations():
    string = input("Enter string: ")
    list1 = list(string)
    permutations = list(itertools.permutations(list1))
    return permutations
permutations()
				
			

Output :

				
					Enter string: name
[('n', 'a', 'm', 'e'),
 ('n', 'a', 'e', 'm'),
 ('n', 'm', 'a', 'e'),
 ('n', 'm', 'e', 'a'),
 ('n', 'e', 'a', 'm'),
 ('n', 'e', 'm', 'a'),
 ('a', 'n', 'm', 'e'),
 ('a', 'n', 'e', 'm'),
 ('a', 'm', 'n', 'e'),
 ('a', 'm', 'e', 'n'),
 ('a', 'e', 'n', 'm'),
 ('a', 'e', 'm', 'n'),
 ('m', 'n', 'a', 'e'),
 ('m', 'n', 'e', 'a'),
 ('m', 'a', 'n', 'e'),
 ('m', 'a', 'e', 'n'),
 ('m', 'e', 'n', 'a'),
 ('m', 'e', 'a', 'n'),
 ('e', 'n', 'a', 'm'),
 ('e', 'n', 'm', 'a'),
 ('e', 'a', 'n', 'm'),
 ('e', 'a', 'm', 'n'),
 ('e', 'm', 'n', 'a'),
 ('e', 'm', 'a', 'n')]
				
			

Related Articles

convert an integer to its word format.

Program to convert an integer to its word format

In this python function program, we will create a function to convert an integer to its word format.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function to_number.
  2. Use the def keyword to define the function.
  3. Take a number as input.
  4. User for loop to iterate over a number after converting it into a string using str() and create an empty string.
  5. Add the words with respect to the number to the empty string.
  6. Use if-elif statements for the purpose.
  7. Print the new string which contains numbers in the word form.
  8. Call the function to get the output.
				
					def to_number():
    num = int(input("Enter a number: "))
    str1 = ""

    for i in str(num):
        if i == "1":
            str1 += "One"
        elif i == "2":
            str1 += "Two"
        elif i == "3":
            str1 += "Three"
        elif i == "4":
            str1 += "Four"
        elif i == "5":
            str1 += "Five"
        elif i == "6":
            str1 += "Six"
        elif i == "7":
            str1 += "Seven"
        elif i == "8":
            str1 += "Eight"
        elif i == "9":
            str1 += "Nine"

    print(str1)
to_number()
				
			

Output :

				
					Enter a number: 2563
TwoFiveSixThree
				
			

Related Articles

get a valid mobile number.

get all permutations from a string.

Program to get a valid mobile number using a function

In this python function program, we will get a valid mobile number using a function. A mobile number should have only 10 integers to be a valid mobile number.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function mobile_number.
  2. Use the def keyword to define the function.
  3. Take the mobile number as input through the user.
  4. If the length of the number is 10 then it is a valid mobile number if not then it is not a valid number.
  5. Use an if-else statement for this purpose.
  6. Print the respective output.
  7. Call the function to get the result.
				
					def mobile_number():
    num = int(input("Enter phone number: "))
    if len(str(num)) == 10:
        print("It is a valid phone number")
    else:
        print("It is not a valid phone number")
mobile_number()
				
			

Output :

				
					Enter phone number: 24568526
It is not a valid phone number
				
			

Related Articles

get the length of the last word in a string.

convert an integer to its word format.

Program to get the length of the last word in a string using a function

In this python function program, we will get the length of the last word in a string using a function. Length means total number of characters/integers present in the word/number.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function length.
  2. Use the def keyword to define the function.
  3. Take a string as input through the user.
  4. Convert the string into a list by splitting the string using split(” “).
  5. Get the length of the last word using the indexing and len() function.
  6. Print the output.
  7. Call the function to get the output.
				
					def length():
    str1 = input("Enter string: ")
    l = str1.split(" ")
    print(f"Length of the last word {l[-1]} in the string: ",len(l[-1]))
length()
				
			

Output :

				
					Enter string: sqatools in best for learning python
Length of the last word python in the string:  6
				
			

Related Articles

search words in a string.

get a valid mobile number.

Program to search words in a string using a function

In this python function program, we will search words in a string using a function.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function search.
  2. Use the def keyword to define the function.
  3. Take a string and a word as input through the user.
  4. Create a count variable and assign its value equal to 0.
  5. Use a for loop to iterate over the words in the string after splitting it using split(” “).
  6. Use an if statement to check whether the word is in the string or not.
  7. If yes then add 1 to the count variable.
  8. Based on the value of the count variable and using an if-else statement determine whether the word is in the string or not.
  9. Print the respective output.
  10. Call the function to get the output.
				
					def search():
    str1 = input("Enter string: ")
    str2 = input("Enter word: ")
    count = 0
    for word in str1.split(" "):
        if word == str2:
            count += 1
    if count > 0:
        print(f"{str2} is in {str1}")
    else:
        print(f"{str2} is not in {str1}") 
search()
				
			

Output :

				
					Enter string: python programming
Enter word: python
python is in python programming
				
			

Related Articles

add two Binary numbers.

get the length of the last word in a string.

Program to add two Binary numbers using a function

In this python function program, we will add two Binary numbers using a function.

Binary Number: A number system where a number is represented by using only two digits (0 and 1) with a base 2 is called a binary number system.

Steps to solve the program
  1. Create a function binary.
  2. Use the def keyword to define the function.
  3. Pass two parameters i.e. binary numbers.
  4. Covert the binary numbers to the integers and then add them.
  5. Convert the addition to the binary number using bin().
  6. To remove the starting 0b from the number using indexing.
  7. Print the output.
  8. Pass the binary numbers to the function while calling the function.
				
					def binary(n1,n2):
    result = bin(int(n1,2)+int(n2,2))
    print(f"Addition of binary numbers {n1},{n2}: ",result[2:]) #to get rid of 0b
binary('100010','101001')
				
			

Output :

				
					Addition of binary numbers 100010,101001:  1001011
				
			

Related Articles

create a library management system.

search words in a string.

Program to create a library management system using a function

In this python function program, we will create a library management system using a function.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function library.
  2. Use the def keyword to define the function.
  3. Pass two parameters i.e. book name and customers name.
  4. Print the book name and customer name.
  5. Pass the book name and customer name to the function while calling the function.
				
					def library(bname,cname):
    print("Book name: ",bname)
    print("Borrowed by :",cname)
library("Harry potter","Atharv")
				
			

Output :

				
					Book name:  Harry potter
Borrowed by : Atharv
				
			

Related Articles

program to reverse an integer.

add two Binary numbers.

Program to reverse an integer using a function

In this python function program, we will reverse an integer using a function.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function reverse.
  2. Use the def keyword to define the function.
  3. Take a number as input through the user and create a variable rev and assign its value equal to 0.
  4. Use a while loop to find the reverse of a number.
  5. While num is greater than 0 divide the number by 10 using % to find the remainder.
  6. Multiply the rev by 10 and add the remainder to it.
  7. Now divide number by 10 using //.
  8. Print the final output.
  9. Call the function to see the output.
				
					def reverse():
    num=n=int(input("Enter a number: "))
    rev=0
    while n>0:
        r=n%10
        rev=(rev*10)+r
        n=n//10
    print("Reverse number: ",rev)
reverse()
				
			

Output :

				
					Enter a number: 225
Reverse number:  522
				
			

Related Articles

check whether the given year is a leap year.

create a library management system.

Program to check whether the given year is a leap year

In this python function program, we will create a function to check whether the given year is a leap year.


Leap Year: A year, occurring once every four years, which has 366 days including 29 February as an intercalary day. A Century year is a leap year only if it is perfectly divisible by 400.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function leap.
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. year.
  4. Use the following condition to check whether the given year is a leap year or not – (year%400 == 0 or year%100 != 0) and year%4 == 0.
  5. Print the respective output.
  6. Pass the year to the function while calling the function.
				
					def leap(a):
    if (a%100!=0 or a%400==0) and a%4==0:
        print("The given year is leap year.")
    else:
        print("The given year is not leap year.")
leap(2005)
				
			

Output :

				
					The given year is not leap year.
				
			

Related Articles

create a Fruitshop Management system.

program to reverse an integer.