Modifying parts of the integer-like string [duplicate] - python

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'

Related

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

Modifying bit string in python [duplicate]

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)

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)

Python convert int to string preserving zeros [duplicate]

This question already has answers here:
How do I pad a string with zeroes?
(19 answers)
Closed 6 years ago.
i'd like to convert a decimal to a string, where zeros at the end are preserved.
Using str method erases the last zeros.
Example:
number=0.20
Goal: "0.20"
e.g. using: str(number)="0.2" doesn't seem to work.
If you want 2 decimal places use:
number = 0.20
str_number = '%.2f' % number
Number before f indicates the desired number of places.
This can be done using string formatting.
"{0:.2f}".format(number)
Will return 0.20.
Doing your chosen method won't work because upon declaring number = 0.20 it omits the last zero right away. If you put that into your idle:
number = 0.20
number
0.2
So declaring number as str(number) is doing str(0.2).
Use the % operator with an appropriate format string:
'%1.2f' % number
=> '0.20'

Converting a string to a numerical value [duplicate]

This question already has answers here:
How to extract a floating number from a string [duplicate]
(7 answers)
Closed 8 years ago.
I wander if there is a simple way to convert a string to a number, knowing that the string begins with numbers but can contain non numerical characters.
For example: my_str = "36.12minuts"
I remember a function in Visual Basic that does the conversion directly :
my_str = "36.12minuts"
val(my_str) => 36.12
How about Python?
def digitsndots(text):
if text in ["1","2","3","4","5","6","7","8","9","0","."]:
return True
else:
return False
num = float(filter(digitsndots, "36.12minuts"))
print num
When using this make sure your string does not have digits in between like "1.a.34.c" (courtesy of #Taha)

Categories

Resources