Format String - number [closed] - python

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 2 years ago.
Improve this question
This is a easy question but i need explanation.
In this code Why the output is 100:
x = 100.1205
y = str(x)[6]
print("{:.{}f}".format(x,y))

x = 100.1205
y = str(x)[6] ## This is turning x into a string and grabbing the 6th character which is 0
print("{:.{}f}".format(x,y)) # this is saying print x to the decimal place
#specified by the value of y (zeroth digit) which is 100

Related

I am trying to convert a string from a list into an integer without losing the decimal places [closed]

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 3 months ago.
Improve this question
I want to convert a string to integer without rounding. For example
s = "99.7"
x = s(int(float(s))
Output:
99
But I want the output to be 99.7
I was thinking of just adding all the strings to a list and somehow converting the list to an integer but I am not sure how to do that or how to even do it individually.
Desired output:
x = '99.7'
z = int(x)
output:
99.7
An integer in python can not have a floating point. To show this you should use
float(x)
This will prevent any rounding.

How are escape sequences contributing to the length of a string? [closed]

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 1 year ago.
Improve this question
a=("\nHello")
print(len(a))
The intended output is a new line with 5 printed but instead there is no new line and the length is coming out to be 6.
There is a new line printed, plus the 5 characters in 'Hello', that's 6 in total:
a = "\nHello"
print(a)
print("Total length is", len(a))
Output
<space>
Hello
Total length is 6

Python integer spacing [closed]

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 have a code like this.
print (' ', totalh, totalt, totalo)
and I want the integer of totalh to be 2 2 instead of 22 with a space in-between the integers. How do I go about doing it?
Convert it to a string which is an iterable and can be an argument to " ".join.
totalh = 22
print(" ".join(str(totalh)))
Output:
2 2
I suggest you this simple way: first convert totalh into a string with each digit being a character, then use " ".join() to insert a space between each digit:
totalh = 22
print(" ".join(str(totalh))) #2 2

Using python how to convert hex byte string to string of hex intergers in flipped order [closed]

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 7 years ago.
Improve this question
From:
25f207f28332c7da539dd2539230a7aad975f907a93d924b4b037abacde677f0f71f75cc250df2e4dd2be3e97a60f289
To:
0x7a60f289, 0xdd2be3e9, 0x250df2e4, 0xf71f75cc, 0xcde677f0, 0x4b037aba,
0xa93d924b, 0xd975f907, 0x9230a7aa, 0x539dd253, 0x8332c7da, 0x25f207f2
Just slice the string into chunks and reverse the order adding 0x with str.format:
s = "25f207f28332c7da539dd2539230a7aad975f907a93d924b4b037abacde677f0f71f75cc250df2e4dd2be3e97a60f289"
print(["0x{}".format(s[i-8:i]) for i in range(len(s), 0, -8) ])
['0x7a60f289', '0xdd2be3e9', '0x250df2e4', '0xf71f75cc', '0xcde677f0', '0x4b037aba', '0xa93d924b', '0xd975f907', '0x9230a7aa', '0x539dd253', '0x8332c7da', '0x25f207f2']
If you actually want decimals:
print([int("0x{}".format(s[i-8:i]),16) for i in range(len(s), 0, -8) ])
[2053173897, 3710641129, 621671140, 4146034124, 3454433264, 1258519226, 2839384651, 3648387335, 2452662186, 1402851923, 2201143258, 636618738]

Create random string with a char + 2 numbers - python [closed]

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 7 years ago.
Improve this question
Two questions:
How to create a char+number random string. one char and 2 digit numbers
for example: U12 OR W82
How to create a random 5 digit number with no zero at the beginning: 02345 , 03243
1)
import random, string
"%c%02d" % (random.choice(string.uppercase), random.randint(0, 99))
2)
import random
random.randint(10000, 99999)

Categories

Resources