Modifying bit string in python [duplicate] - python

This question already has answers here:
Replace first occurrence of string in Python
(2 answers)
Closed 4 years ago.
Currently, i have a bit string represented as
current = "011"
and what I'm trying to do is to create a new string based of the bit string above with the 1 at index 1 replaced with 011 which would give me:
new = "00111"
The problem I'm having is that when I use the replace function, it replaced all the 1 in the string including the one at index 2 which is not what I desired.
new = current.replace("1","011")
= 0011011 #not what I wanted
Would appreciate some help on this.

Limit the number of replace to 1 such as below:
new = current.replace("1","011", 1)

Related

String index out of range-python while using slice operator [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 1 year ago.
Here as we can see in the image when we slice the string from 0:len(s) we are getting the full string, but when I try to print s of len (s) instead of getting the last character it throws me n error.
I'm new to python so have some mercy! thanks!!
s='hello'
print(s[0:len(s)])
print(s[len(s)])
The string length is always 1 more than the max index since index is starting from 0 for single character while length starts with 1 for single character.
Please change your code to be like this:
s='hello'
print(s[0:len(s)-1])
print(s[len(s)-1])

Split string into two integers, python [duplicate]

This question already has answers here:
Given n, take tsum of the digits of n. If that value has more than one digit, continue reducing a single-digit number is produced
(4 answers)
Closed 1 year ago.
I have problem and trying to get next:
new_string = "35" #and this result must be like new_int = 3+5.
How im available to do this? I know the type conversion, but not a clue how i should do this.
As you are new to the python, i suggest you doing it using
int(new_string[0]) # 3
int(new_string[1]) # 5
So now you have 2 integers, you can to whatever you want

Getting value component in python? [duplicate]

This question already has answers here:
Python - Split integer into individual digits (undetermined amount of digits)
(4 answers)
Closed 1 year ago.
I cannot describe clearly because English is not my native language
If I input like this
a = 4252
Then I want to take each number component, it should be like this
a1=4; a2=2; a3=5; a4=2
How to do that in python?
If you know it has a similar question in stackoverflow, give me a link
Convert the integer to a string for easy iterability then get the int value of every character like this: a1,a2,a3,a4 = [int(elem) for elem in str(a)]

Python - why are my numbers not storing correctly [duplicate]

This question already has answers here:
How to convert a string of space- and comma- separated numbers into a list of int? [duplicate]
(6 answers)
How to split a string of space separated numbers into integers?
(9 answers)
Closed 4 years ago.
I am trying to take in a string of numbers, such as:
1 2 3 4 55 33 15
and store them in a list as they appear so that I can operate on the items at each index in the list, but they aren't splitting and storing right. They store like:
1,2,3,4,5,5,3,3,1,5
which is not what I want. My current code is attached below. I thought they would store as:
1,2,3,4,5,55,33,15
I do convert them to ints later on and that works fine, this is just the part that I am stuck on.
numbersArrayTemp.append(userInput.readline().strip(' ').strip('\n'))
for item in numbersArrayTemp:
print(item)
Type = item.strip('\n')
print(Type)
j = item.replace(" ", '')
numbersArrayString.extend(j)

Modifying parts of the integer-like string [duplicate]

This question already has answers here:
How to pad a string with leading zeros in Python 3 [duplicate]
(5 answers)
Closed 5 years ago.
For any k-digit integer i, the goal is to produce an m-digit string where the first n digits, where n=m-k are zeros, say. Using python would be helpful.
For example, given m=5 and i=324, how to produce "00324"?
EDIT:
The zfill function pads the integer with zeros. Is there any more general function that pads the integer with an arbitrary integer/character?
You can use zfill
i = 324
m = 5
s = str(i).zfill(m)
# '00324'

Categories

Resources