Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Replace 0’s with 1’s using Python

Replace all 0’s with 1’s

In this program, we will replace all 0’s in an integer with 1’s in python. First, we will convert the integer to string and then iterate through the string and check every character for 0’s and then replace it with 1’s.Finally we will display the output string.

  • Example : n=60705,
    • Replace all 0’s with 1’s in 60705 becomes 61715
    • Final result 61715
replace all 0's with 1 using python

Algorithm:

  • Input number and initialise empty list
  • Convert integer to string
  • Find all 0’s in the string and replace it with 1’s and append to list
  • Finally convert the list to string
  • Print string by converting to integer

Python code:

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

Output:

Enter any number 8500
8511