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)
Related
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
given the number 12345 = 1x10^4 + 2x10^3 + 3x10^2 + 4x10^1 + 5x10^0, how to perform some arithmetic operations to leave you with just the digit at position 5 (from the left) and output it to the screen? Thanks for the help!
Assuming you want to stick to arithmetic operations (and not strings), use the modulo operator with 10 to get the remainder of division by 10, i.e. the unit:
12345%10
output: 5
For an arbitrary number, you need to compute the position, you can use log10 and ceil:
from math import log10, ceil
N = 5
number = 1234567
number//10**(ceil(log10(number))-N)%10
output: 5
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
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
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
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]