Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I expected result is sometimes getting padded with 000 or 0000 or 0
Expected : 2
Actual : 0002 or 02
The comparison is failing where it should pass.
If you're dealing with numbers, use Should Be Equal As X, where X is either Numbers if they are floats, or Integers - if whole numbers. The keyword will cast the compared values, and do comparison where the padding doesn't matter:
Should Be Equal As Numbers 002.9080 000002.908 # will pass
If any of the arguments cannot be casted - they have a letter inside them, for example, the keyword will fail.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 months ago.
Improve this question
I have a text file containing bits so it’s like „1000101011010110000…“ in this text. I want python to interpret this text as bytes and perform different byte transformations with it. But how do I red it on as bytes without python thinking it’s a string?
The built-in int function has an parameter base for specifying the base.
To convert a string into an integer with base 2 (binary), pass 2 into it:
s = input()
num = int(s, 2)
# Manipulate `num` as you like
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 months ago.
Improve this question
I'm struggling to implement a program that prompts the user for an arithmetic expression,
(for example; 1 + 1) and then calculates and outputs the result as a floating-point value formatted to one decimal place (for example; 2.0).
Also, I want there to be a space between x and y, and a space between y and z in the equation (for example; 1 + 1, with spaces between each character).
This is basically my desired output (in a terminal):
https://i.stack.imgur.com/5LuRH.png
I'm pretty new to python so this will probably seem easier to more experienced coders, but anyone have any ideas, and thanks in advance.
You can just simply use eval directly on the expression inserted...
expression = str(input('Expression: '))
print("{:.1f}".format(eval(expression), 1))
output:
Expression: 1 + 1
2.0
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I need help, in a long string, I want to know the the 2 characters after a number
example = "gz15gr123da1az4gr1sd23f168zgre4r6z"
if in example after 1 number == "gr":
so delete "gr"
If I understand your question correctly, what you need is regex:
import re
example = "gz15gr123da1az4gr1sd23f168zgre4r6z"
re.sub("(\d)gr", r"\1", example)
Output
gz15123da1az41sd23f168zgre4r6z
Explnatation
The re.sub function takes three arguments: the first argument is a patter, the second one the replacement, and the last of is the string itself. (\d)gr selects the parts that contain a number followed by gr, then replaces it with the number (\d) itself(removing the gr part).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need to add the integers from double digit number(triple etc.) together
Block quote 22 = 2+2 = 4
I considered doing it manually but because I have 999 number to get through that'll take a really long time.
I tried making two separate lists and adding it together but continually received syntax errors.
*edited for extra detail *
You can convert the integer to a string, and then sum the values up:
count = sum((int(digit) for digit in str(22)))
print(count)
This iterates over every digit, converts it back to an integer, and finally computes the sum over all digits.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Basically what I'm asking is, what's the most direct way to convert any integer between 0 and 255 into it's hexadecimal, escaped equivalent? One that I mean will function correctly if wrapped in a write() function (which means '\x56' writes 'V' and not literally '\x56'.
That's what the chr function is for.
f.write(chr(0x56))
Speaking of hexadecimal escaped equivalents isn't really relevant in this context - every character has a hexadecimal equivalent, but in expressing a string the characters that can be expressed as a single simple character are simply output as the character.