Split string into two integers, python [duplicate] - python

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

Related

How to format Python code to always return this specific length [duplicate]

This question already has answers here:
Dynamically calculated zero padding in format string in python
(2 answers)
How do I pad a string with zeroes?
(19 answers)
Closed 9 months ago.
Sorry if this is a bit of a noob question. But moving on..
Say at the beginning of my code I set a variable, like this:
TestVar = 'A6'
But I later want it to print out as 000000A6
Or say it was
TestVar = 'C30'
I'd want it to print out as 00000C30
Basically always returning it with a length of 8
The reasoning for this is I've made a general script for modding a game, (I can link if asked) and you need to put in certain values which I want to have format automatically for ease of use. For example on running it'll print
Item ID Here:
And if you put in 166 it would convert the decimal number to hex which would be A6, however in order to be usable for all values (not just ones that are 2 digits once converted) I'm trying to make it detect it's length and format it with the 0s before.
Sorry if this doesnt make sense, in a simpler way of saying this, is there a way for it to detect the length of a variable? So for example in pseudo
TestVar = 'C30'
If TestVar length = 3
print('00000'+TestVar)
Print Result: 00000C30
Basically always returning it with a length of 8
That's what format strings do:
>>> print(f"{'C30':>08s}")
00000C30
As a sidenote, to output any number as 8-digit hex:
>>> print(f"{100:>08X}")
00000064
>>> print(f"{1024:>08X}")
00000400
See the documentation:
for f-strings (the f'I am an f-string' syntax);
for formatting syntax (the >08s and >08X thing).
Use string function rjust():
print(test.rjust(8,'0'))
The .zfill string method can be used.
For example:
s = 'C30'
s.zfill(8)
>>> '00000C30'
Try this code
txt = "A6"
x = txt.zfill(8)
print(x)
You can use string.zfill method
for example.
code = '3C0'
filledCode = code.zfill(8)
this method filled with zero the number of digit that you pass like a parameter
try something like this str.rjust() function
i = 1111
pad = '0'
n = 8
x = str(i).rjust(n, pad)
print(x) # 00001111

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: is there a way to 'cleanly' divide two numbers of type float and int? [duplicate]

This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Closed 3 years ago.
Pretty new to python, facing a problem that requires basically the opposite of the remainder "%" function. For example, if I wanted to divide 81.5 by 20, my output would be 4. My best attempt is as follows:
amount = 81.504
round(amount, 2)
num20s = amount / 20
int(num20s)
I've tried several different combinations of the above code, but nothing has worked so far. This is the closest I've gotten to what I want, but it won't work in edge cases, and for some reason still represents the number with a ".0" at the end, so that last line must not be doing anything.
Integer division operator in python is "//".
>>> amount = 81.504
>>> amount // 20
Out[3]: 4.0
>>> int(amount // 20)
Out[4]: 4

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)

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