Python Program for Replace All 0’s With 1 In A Given Integer

Replace All 0’s With 1 In A Given Integer

An integer is a primitive datatype in python and a very basic type used in almost every programing language. In this python program, we will replace all the 0’s from an integer by 1. We will use a simple approach to replace all 0’s. by using an in-built function replace function.

replace all 0's with 1 using python

Algorithm

  • Step 1:- Start.
  • Step 2:- Convert the type of integer to string by str(Val).
  • Step 3:- Use replace function that takes 2 arguments,replace(0,1).
  • Step 4:- Print replaced output.
  • Step 5:- End.

Python program to replace 0 with 1

Method 1 :

#taking Input
n=int(input())
#converting into string
n=str(n) 
#then into the list
n=list(n)
r= #empty string for addind it the item of list
for i in range(len(n)):
    #if we find ‘0’ we will replace it with ‘1’
    if(n[i]==‘0’):
        n[i]=‘1’
    r=r + n[i]  #creating the new integer 
del n    
print(int(r))
    

Method 2 :

n=int(input(“Enter any number”))
s=str(n)
l=[]
for i in s:
    if(i==‘0’):
        l.append(‘1’)
    else:
        l.append(i)
ns=“”
for i in l:
    ns+=i
print(int(ns))
 

Method 3 :

#take inputs
Val = int(input('Enter the number :'))
#change type to string
Val = str(Val)
Replaced = Val.replace('0','1')
print('Number after replacement is :' + Replaced)
Output:
Enter the number :072204
Number after replacement is :172214
Input:

12090

Output:

12191